Using GnuplotRB in IRuby notebooks

GnuplotRB plots may be embedded into iRuby notebooks as images (terminals svg, png, jpeg and pngcairo are supported) or plain text (dumb terminal). By default GnuplotRB will use svg:

In [1]:
require 'gnuplotrb'
include GnuplotRB

simple_plot = Plot.new(
  ['x*sin(x)', with: 'lines', lt: { rgb: 'blue', lw: 3 }],
  xrange: -10..10,
  title: 'Math function example', 
  ylabel: 'x',
  xlabel: 'x*sin(x)'
)
Out[1]:
Gnuplot Produced by GNUPLOT 5.0 patchlevel rc2 -6 -4 -2 0 2 4 6 8 -10 -5 0 5 10 x x*sin(x) Math function example x*sin(x) x*sin(x)

You can make GnuplotRB plots to be rendered into other formats using term option:

In [2]:
simple_plot.term('png')
Out[2]:
In [3]:
simple_plot.term('jpeg')
Out[3]:
In [4]:
simple_plot.term('dumb')
Out[4]:

                             Math function example

  8 +-+---------------+-----------------+----------------+---------------+-+
    +     +  +        +                 +                +        +  +     +
  6 +-+  +   +                                            x*sin(x)++-----+-+
    |   +     +                                                  +     +   |
    |   |      +                                                +      |   |
  4 +-+ +      |                                                |      + +-+
    |   |      +                                                +       |  |
  2 +-++        +                                              +        ++-+
    |  |         |            ++++++        ++++++             |         | |
    | +          +           +      +++  +++      +           +          + |
  0 +-+           |         +         ++++         +          |          +-+
    ||            +        +                        +        +            ||
 -2 +-+           +       +                          +       +           +-+
    ||             +      +                          +      +             ||
    |+              +    +                            +    +              +|
 -4 +-+             ++ ++                              ++ ++             +-+
    +                 +                 +                +                 +
 -6 +-+---------------+-----------------+----------------+---------------+-+
   -10               -5                 0                5                 10
                                   x*sin(x)

Using terminal options

To specify some image params like canvas size or font you can pass options to term:

In [5]:
simple_plot.term('dumb', size: [60,30])
Out[5]:

                    Math function example

  8 +-+----------+------------+------------+----------+-+
    +   + +      +            +            +      + +   +
    |   |  +                           x*sin(x) +-----+ |
  6 +-+ +  |                                     |  + +-+
    |   |  +                                     +   |  |
    |  +    |                                    |   +  |
    |  |    +                                   +    |  |
  4 +-++    |                                   |    ++-+
    |  |    +                                   +    |  |
    | |      |                                  |     | |
  2 +-+      +          +++       +++          +      +-+
    | |      |         +  ++     ++  +         |      | |
    | |      +        +    ++   ++    +        +      | |
  0 +-+       |       +      +++      +        |      +-+
    | |       +      +                 +      +       | |
    ||         |     +                 +      |        ||
 -2 +-+        +     |                  |    +        +-+
    ||         |    +                   +    |         ||
    ||         +    +                   +    +         ||
    |+          +  +                     +  +          +|
 -4 +-+         +  +                     +  +         +-+
    |            ++                       ++            |
    +            +            +            +            +
 -6 +-+----------+------------+------------+----------+-+
   -10          -5            0            5            10
                          x*sin(x)

In [6]:
simple_plot.term('svg', fsize: '18')
Out[6]:
Gnuplot Produced by GNUPLOT 5.0 patchlevel rc2 -6 -4 -2 0 2 4 6 8 -10 -5 0 5 10 x x*sin(x) Math function example x*sin(x) x*sin(x)

Of course plot may be created with term option already set:

In [7]:
Splot.new(
  ['sin(u)*cos(v), sin(u)*sin(v), cos(u)', title: 'Sphere'],
  parametric: true,
  urange: 0..Math::PI,
  vrange: 0..2*Math::PI,
  title: 'sphere',
  hidden3d: true,
  isosamples: 30,
  term: ['png', size: [600, 800]]
)
Out[7]:

Plotting to image file

GnuplotRB allows to plot into png/svg/jpeg/gif files with handy #to_|format name| methods. They take 2 arguments: path to image file (optional, if none given, file contents will be returned from method) and options. This kind of methods are available for all Plottable objects except Animation (Dataset, Plot, Splot, Multiplot). You may see all possible formats in gnuplot doc p. 190. You can also check which terminals are handled by your gnuplot installation:

In [8]:
Settings.available_terminals
Out[8]:
["cairolatex", "canvas", "cgm", "context", "corel", "dumb", "dxf", "eepic", "emf", "emtex", "epscairo", "epslatex", "fig", "gif", "hpgl", "jpeg", "latex", "lua", "mf", "mp", "pcl5", "pdfcairo", "png", "pngcairo", "postscript", "pslatex", "pstex", "pstricks", "qms", "qt", "svg", "tek40xx", "tek410x", "texdraw", "tgif", "tikz", "tkcanvas", "tpic", "unknown", "vttek", "x11", "xlib", "xterm"]

Examples

In [9]:
Plot.new('sin(x)').to_png('plot.png')
File.open('plot.png')
Out[9]:
In [10]:
svg_contents = Plot.new('sin(x)').to_svg(size: [600, 600])
IRuby.display(svg_contents, mime: 'image/svg+xml')
Gnuplot Produced by GNUPLOT 5.0 patchlevel rc2 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -10 -5 0 5 10 sin(x) sin(x)