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:
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)'
)
You can make GnuplotRB plots to be rendered into other formats using term option:
simple_plot.term('png')
simple_plot.term('jpeg')
simple_plot.term('dumb')
Math function example 8 +-+---------------+-----------------+----------------+---------------+-+ + + + + + + + + + 6 +-+ + + x*sin(x)++-----+-+ | + + + + | | | + + | | 4 +-+ + | | + +-+ | | + + | | 2 +-++ + + ++-+ | | | ++++++ ++++++ | | | | + + + +++ +++ + + + | 0 +-+ | + ++++ + | +-+ || + + + + || -2 +-+ + + + + +-+ || + + + + || |+ + + + + +| -4 +-+ ++ ++ ++ ++ +-+ + + + + + -6 +-+---------------+-----------------+----------------+---------------+-+ -10 -5 0 5 10 x*sin(x)
To specify some image params like canvas size or font you can pass options to term:
simple_plot.term('dumb', size: [60,30])
Math function example 8 +-+----------+------------+------------+----------+-+ + + + + + + + + + | | + x*sin(x) +-----+ | 6 +-+ + | | + +-+ | | + + | | | + | | + | | | + + | | 4 +-++ | | ++-+ | | + + | | | | | | | | 2 +-+ + +++ +++ + +-+ | | | + ++ ++ + | | | | | + + ++ ++ + + | | 0 +-+ | + +++ + | +-+ | | + + + + | | || | + + | || -2 +-+ + | | + +-+ || | + + | || || + + + + || |+ + + + + +| -4 +-+ + + + + +-+ | ++ ++ | + + + + + -6 +-+----------+------------+------------+----------+-+ -10 -5 0 5 10 x*sin(x)
simple_plot.term('svg', fsize: '18')
Of course plot may be created with term option already set:
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]]
)
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:
Settings.available_terminals
["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"]
Plot.new('sin(x)').to_png('plot.png')
File.open('plot.png')
svg_contents = Plot.new('sin(x)').to_svg(size: [600, 600])
IRuby.display(svg_contents, mime: 'image/svg+xml')