Placing multiple plots on single layout

Gnuplot have special layout for placing multiple plots on single layout and GnuplotRB supports this feature as well.


In [1]:
require 'gnuplotrb'
include GnuplotRB

sinx = Plot.new('sin(x)', title: 'Sin')
exp = Plot.new('exp(x)', title: 'Exp')
log = Plot.new('log(x)', title: 'Log')
sphere = 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
)
nil

To use this feature in GnuplotRB you just need to pass several Plot or Splot objects to constructor of Multiplot class. It's recommended to give him layout size too.


In [2]:
rows, cols = 2, 2
basic_four = Multiplot.new(sinx, exp, log, sphere, layout: [rows, cols])


Out[2]:
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 sin(x) sin(x) 0 5000 10000 15000 20000 25000 -10 -5 0 5 10 Exp exp(x) exp(x) -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 -10 -5 0 5 10 Log log(x) log(x) sphere gnuplot_plot_1d gnuplot_plot_2d Sphere -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1 -0.5 0 0.5 1

You can also set one title for the whole layout.


In [3]:
basic_four.title('Multiplot example')


Out[3]:
Gnuplot Produced by GNUPLOT 5.0 patchlevel rc2 Multiplot example -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 sin(x) sin(x) 0 5000 10000 15000 20000 25000 -10 -5 0 5 10 Exp exp(x) exp(x) -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 -10 -5 0 5 10 Log log(x) log(x) sphere gnuplot_plot_1d gnuplot_plot_2d Sphere -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1 -0.5 0 0.5 1

You can change layout size anytime (if it will have less cells than you have plots, they will be overriden by each other).


In [4]:
basic_two = Multiplot.new(sinx, sphere, layout: [1,2])


Out[4]:
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 sin(x) sin(x) sphere gnuplot_plot_1b gnuplot_plot_2b Sphere -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1 -0.5 0 0.5 1

In [5]:
basic_two.layout(2,1)


Out[5]:
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 sin(x) sin(x) sphere gnuplot_plot_1b gnuplot_plot_2b Sphere -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1 -0.5 0 0.5 1

You can also use size and origin options instead of layout. It's useful when you need to have different cells.


In [6]:
minisinx = sinx.options(size: [0.6,1.0], origin: [0,0])
minilog = log.options(size: [0.4,0.5], origin: [0.6,0])
minisphere = sphere.options(size: [0.4,0.5], origin: [0.6,0.5])
Multiplot.new(minisinx, minilog, minisphere)


Out[6]:
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 sin(x) sin(x) -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 -10 -5 0 5 10 Log log(x) log(x) sphere gnuplot_plot_1c gnuplot_plot_2c Sphere -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1 -0.5 0 0.5 1