Skip to content
YAMAMOTO, Masayuki edited this page Jun 8, 2023 · 53 revisions

GR::Plot - A simple, matlab-style API

  • Currently the plot attributes are provided as a keyword argument.
  • This page is besed on GRUtils.jl.

Getting ready

We will add keyword arguments to two simple graphs: one is a simple 2D line plot and the other is a 3D wireframe. Here, we use Rdatasets Gem to load the data.

Simple 2D line plot

require 'gr/plot'
require 'rdatasets'

passenger = RDatasets.load(:datasets, :AirPassengers)
time = passenger["time"]
value = passenger["value"]

GR.plot(time, value)

plain

Simple 3D wireframe

volcano = RDatasets.datasets.volcano.to_matrix.to_a.transpose
volcano = Numo::DFloat.cast(volcano)

GR.wireframe(volcano)

volcano

Axis guides

title

Set the plot title as the string.

GR.plot(time, value, title: 'Air Passenger numbers from 1949 to 1961')

title

figsize

GR.plot(time, value, figsize: [1, 1])

figsize

GR.plot(time, value, figsize: [6, 2])

figsize

size

Set the approximate size of the window on the screen to be displayed in pixel size. The default size is [600, 450].

GR.plot(time, value, size: [480, 360])

size

xlabel, ylabel, zlabel

Set the X, Y or Z axis labels as the string.

GR.plot(time, value,
        title: 'Air Passenger numbers from 1949 to 1961',
        xlabel: 'Date',
        ylabel: "Passenger numbers (1000's)")

xlabel and ylabel

GR.wireframe(volcano, title: "Maunga Whau (Mt Eden)",
                      xlabel: "X", ylabel: "Y", zlabel: "Z")

xyzlabels

grid

Draw or disable the grid of the current plot axes.

GR.plot(time, value, grid: false)

grid

GR.wireframe(volcano, grid: false)

grid

xticks yticks zticks

Set the minor intervals of the ticks for the X, Y or Z axis, and (optionally) the number of minor ticks between major ticks.

GR.plot(time, value, xticks:[0.5, 4], yticks: [50, 2])

xtics 2D

GR.wireframe(volcano, xlabel: "X", ylabel: "Y", zlabel: "Z",
                      xticks: 11,  yticks: 13,  zticks: 17)

xtics 3D

xticklabels yticklabels

  • Under development

Axis dimensions

xlim ylim zlim

Set the limits for the plot axes.

GR.plot(time, value, ylim: [0, 4000], xlim: [1920, 2020])

lim

GR.wireframe(volcano, zlim: [80, 120])

lim

GR.wireframe(volcano.clip(80,120), zlim: [80, 120])

image

panzoom

Pan/zoom the axes of the current plot.

3-D views

viewport

Set the viewpoint of three-dimensional plots.

rotation

Rotate the viewpoint of the current plot by angle degrees around the vertical axis of the scene, with respect to its current position.

30.times do |i|
  GR.wireframe(volcano, rotation: i * 3)
end

rotation

tilt

Tilt (elevate) the viewpoint of the current plot by angle degrees over the horizontal plane, with respect to its current position.

30.times { |i|  GR.wireframe(volcano, tilt: i * 3) }
30.times { |i|  GR.wireframe(volcano, tilt: 90 - i * 3) }

tilt

Not implemented: movefocus, turncamera

Axis scales

xflip, yflip, zflip

Reverse the direction of the X-axis, Y-axis or Z-axis, or set them back to their normal direction.

GR.plot time, value, GR.subplot(2,2,1, xflip: false, yflip: false, label: "default",    location: 2)
GR.plot time, value, GR.subplot(2,2,2, xflip: true,  yflip: false, label: "x flipped",  location: 1)
GR.plot time, value, GR.subplot(2,2,3, xflip: false, yflip: true , label: "y fipped",   location: 3)
GR.plot time, value, GR.subplot(2,2,4, xflip: true,  yflip: true , label: "x,y fipped", location: 4)

flip 2D

GR.wireframe volcano, GR.subplot(2,2,1, xflip: false, yflip: false)
GR.wireframe volcano, GR.subplot(2,2,2, xflip: true,  yflip: false)
GR.wireframe volcano, GR.subplot(2,2,3, xflip: false, yflip: true )
GR.wireframe volcano, GR.subplot(2,2,4, xflip: true,  yflip: true )

flip 3D

xlog, ylog, zlog

Set the X-axis, Y-axis or Z-axis to be drawn in logarithmic scale, or in linear scale.

GR.plot time, value, GR.subplot(2,1,1, ylabel: "Y")
GR.plot time, value, GR.subplot(2,1,2, ylabel: "ylog", ylog: true)

ylog

GR.wireframe volcano xlog: true

xlog

GR.wireframe volcano ylog: true

ylog

GR.wireframe volcano xlog: true, ylog: true

xylog

GR.wireframe volcano -100, zlog: true

zlog

complete nonsense.

Not implemented: radians

Geometry guides

location

Set the legend of the plot, using a series of labels.

In GRUtils legend

location location location location

Not implemented: geometrykinds, colorbar

Colors

backgroundcolor

Add a custom background color to the current figure.

GR.plot(time, value, "wp", backgroundcolor: 1)

image

colormap

Set or retrieve the values of the current colormap.

The following examples are useful for checking the colormap in your environment.

colormap

GR.contourf(volcano, colormap: 40)

colormap

GR.contourf(volcano, colormap: 1)

colormap

Font

GR::Plot::FONT

Name
Times_Roman 101
Times_Italic 102
Times_Bold 103
Times_BoldItalic 104
Helvetica_Regular 105
Helvetica_Oblique 106
Helvetica_Bold 107
Helvetica_BoldOblique 108
Courier_Regular 109
Courier_Oblique 110
Courier_Bold 111
Courier_BoldOblique 112
Symbol 113
Bookman_Light 114
Bookman_LightItalic 115
Bookman_Demi 116
Bookman_DemiItalic 117
NewCenturySchlbk_Roman 118
NewCenturySchlbk_Italic 119
NewCenturySchlbk_Bold 120
NewCenturySchlbk_BoldItalic 121
AvantGarde_Book 122
AvantGarde_BookOblique 123
AvantGarde_Demi 124
AvantGarde_DemiOblique 125
Palatino_Roman 126
Palatino_Italic 127
Palatino_Bold 128
Palatino_BoldItalic 129
ZapfChancery_MediumItalic 130
ZapfDingbats 131
CMUSerif-Math 232
DejaVuSans 233
PingFangSC 234