This post is part of a series on Gnuplot, see the previous post Introduction to Gnuplot (Part II)
There are situations when you want to plot compound graphs. Compound graphs are needed to plot data files with multiple columns. In this post we use an example data file with columns. An x value (column #1), x^3 value (column #2) and x^5 value (column #3).
#x x^3 x^5 1 1 1 1.1 1.331 1.61051 1.2 1.728 2.48832 1.3 2.197 3.71293 1.4 2.744 5.37824 1.5 3.375 7.59375 1.6 4.096 10.48576 1.7 4.913 14.19857 1.8 5.832 18.89568 1.9 6.859 24.76099
You may want to download the data file from here.
using Modifier in plot command:
using modifier has many usages but they are not covered here. For our example, using tells gnuplot which column from the data file it should use to plot a graph.
It takes arguments as using <xcol>:<ycol>:<zcol>. If only one argument is supplied <xcol> defaults to 1 and supplied argument is taken as <ycol> value. See the example below:
Above code will plot column #1 on X and column #2 on Y.
Here we want two different graphs for x -> x^3 and x->x^5, plot command for will be:
plot 'powers.dat' using 1:2 title 'x^3' with lines, \ 'powers.dat' using 1:3 title 'x^5' with lines
At the end of line #1 you see , \ these symbols tell gnuplot that it should not start plotting and wait for more instructions. On line #2 there is no plot word at the beginning as we are continuing with line #1. We do not use , \ on line #2 because it is the last instruction in this case.
Now, we need a overall title for the graph which can be given using set (more on it later) command:
set title 'Powers of x'
We need to execute set command before plot command, so the actual sequence of commands would be:
set title 'Powers of x' plot 'powers.dat' using 1:2 title 'x^3' with lines, \ 'powers.dat' using 1:3 title 'x^5' with lines
And our first compound graph is ready!