Gnuplot multiplot - seamless plots tips
November 11th, 2009Although creating a basic multiplot in gnuplot is as siple as doing
set multiplot layout 2,1 plot x plot x**2 unset multiplot
one quickly runs into a number of questions, like how to have plots touching each-other and how to force equal plot area sizes? The main trick is in the border size. To create a basic 2×2 plot grid we might have the following:
set multiplot layout 2,2 plot x plot x lc rgb 'blue' plot x lc rgb 'yellow' plot x lc rgb 'orange' unset multiplot
To have the plots touch each-other we change the plot code to:
set rmargin 0 set bmargin 0 plot x set rmargin set lmargin 0 plot x lc rgb 'blue' set rmargin 0 set tmargin 0 set bmargin set lmargin plot x lc rgb 'yellow' set rmargin set lmargin 0 plot x lc rgb 'orange'
As you can see, the plots are nicely touching but several things are off: the tick labels, axis labels and most importantly: the sizes of the plots. To get the size right we start off by specifying a rectangular sized terminal, for example:
set term png size 500,500
The plotting code is changed into:
set multiplot # set size of individual plots so everything fits on the page set size 0.35,0.35 # set the margins to zero set lmargin 0 set rmargin 0 set bmargin 0 set tmargin 0 set xl 'x' set yl 'y' set origin 0.15, 0.5 plot x set origin 0.5, 0.5 plot x lc rgb 'blue' set origin 0.15, 0.15 plot x lc rgb 'yellow' set origin 0.5, 0.15 plot x lc rgb 'orange' unset multiplot
To get the tick and axis labels correct we have to dig in and do some manual labour:
set multiplot # set size of individual plots so everything fits on the page set size 0.35,0.35 # set the margins to zero set lmargin 0 set rmargin 0 set bmargin 0 set tmargin 0 set ylabel 'y' set xlabel '' set format x '' set origin 0.15, 0.5 plot x set ylabel '' set format y '' set origin 0.5, 0.5 plot x lc rgb 'blue' set xlabel 'x' set ylabel 'y' set format x '% g' set format y '% g' set origin 0.15, 0.15 plot x lc rgb 'yellow' set ylabel '' set format y '' set origin 0.5, 0.15 plot x lc rgb 'orange' unset multiplot
Everything here is as we want it, apart from overlapping tick labels. I’ve used 2 methods of dealing with this, both are far from ideal. We can either use a dirty hack and slightly adjust the x or y range of a plot so gnuplot does not draw the final tick label:
set ylabel [:9.9]
Or we can unset the ticks and then manually add them using set xtics/ytics.
unset xtics set xtics (-10, -5, 0, 5)
Whichever method you use, you should with some effort be able to get a result like:
Which is what we started out for : ) It would be great if gnuplot would offer this kind of functionality built-in because creating this kind of plot is, although possible, not that trivial.




