Gnuplot multiplot - seamless plots tips

November 11th, 2009

Although 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.

Gnuplot default plotting style

November 8th, 2009

The default plotting style for gnuplot is not always fantastic, in particular the default line widths are a bit fine and basic tic settings seem to attempt to save as much printer ink as possible by choosing a very limited number of tics. And while we’re at it we want the default plotting style to be lines, not points. The solution: create a .gnuplot file in your home directory:

# .gnuplot, in ~
set mxtics 5
set mytics 5
set xtics scale 2,1
set ytics scale 2,1
 
# Lines, dammit!
set style data lines
 
linew = 1.5
set style line 1 lw linew
set style line 2 lw linew
set style line 3 lw linew
set style line 4 lw linew
set style line 5 lw linew
set style line 6 lw linew lc rgb "forest-green"
set style line 7 lw linew lc rgb "gray50"
set style line 8 lw linew
set style line 9 lw linew
set style line 10 lw linew
 
set style increment user

When running gnuplot interactively .gnuplot is loaded automatically. When scripting gnuplot just start off scripts with

reset
load '~/.gnuplot'

And you’re all set!