Trix & Graphix

Array of plots in Gnuplot

In this post I will explain how to create an array of plots. This may be useful for example if you need to merge several plots in the same figure in a paper. Within this post, by figure I will mean a file which in general may contain several independent plots. The aim of this post is to explain how to get the next figure:


For this purpose, gnuplot has the multiplot environment. To use it, you just have to type multiplot once you are already in gnuplot. From that moment on, each time you type the command plot, you will get a new plot added to the same figure. You have to take into account that every new plot is independent of the former ones. This means that you can (or you have to) reset again all parameters you want to change for the next plot. If you don't reset them, they will remain as in the previous call to plot command. For example, if you set xlabel to "variable x", all plots will have in common this label for their x axis. Of course you can change this by just typing sex xlabel "whatever" always before to use the next plot command. For this reason, it's a good idea to set all common parameters to every plots in the figure before initiating the multiplot environment.

I think the next example is quite self-explanatory, so just one thing has to be remarked. In the multiplot environment you can use an automatic layout for the plots or a manual one. If you want to create a regular array of plots, it makes more sense to use the first option. In the next post I will put an example of how to use the manual approach for a more complex result.

gnuplot << TOEND
set terminal postscript eps color enhanced
set output 'multiplot.eps'

# Some common options
set xrange [-pi:pi]
set mxtics 2
set ytics 0.25
set xtics ("-{/Symbol p}" -pi, "-{/Symbol p}/2" -pi/2, "0" 0,"{/Symbol p}/2" pi/
2, "{/Symbol p}" pi)
set format y "%.2f"

set grid

# This begins the multiplot environment. This example uses a 2x2 regular layout
set multiplot layout 2,2 title 'Using layout 2x2


# Now, every time I use the plot command, I will get a new plot
# in the correct position acording to the selected layout

set title 'Plot 1'
plot sin(1*x) w l lt 1 lc 1 lw 3 t ''

set title 'Plot 2'
plot sin(2*x) w l lt 1 lc 2 lw 3 t ''

set title 'Plot 3'
plot sin(3*x) w l lt 1 lc 3 lw 3 t ''

set title 'Plot 4'
plot sin(4*x) w l lt 1 lc 4 lw 3 t ''

# It is important to close the multiplot environment before leave!!!
unset multiplot

TOEND

0 Comments: