gnuplot is a nice program for quickly making plots. Here we quickly list a number of useful features (the ones that I most often use). Note that we concentrate on plotting data from files rather than plotting mathematical functions.
To generate a plot as a png file of size 640 by 480 using the courier font with size 8:
set terminal png size 640,480 font cour 8
To generate a plot as a colour postscript file, with solid lines and enhanced text (e.g. superscripts and subscripts can be included):
set terminal postscript enhanced color solid
Note that portrait is used by default. For landscape, use this:
set terminal postscript enhanced color solid landscape
To set the output file:
set output "plot.png"
To specify the x-axis range:
set xrange [0:5]
To specify the y-axis range:
set yrange [0:100]
To set the number of x-axis minor ticks to 8, for example:
set mxtics 8
To have a date/time x-axis:
set xdata time
set timefmt "%Y-%m-%dT%H:%M:%S"
This is useful for a data file using the format YYYY-MM-DDTHH:MM:SS. To only show years and months only in the plot x-axis:
set format x "%Y-%m"
This is useful if showing the full date/time value takes up too much space, or if showing the full timestamp is not necessary (for example, if a plot only covers a period of 20 minutes, you might not want to show years, months, days and hours).
Note that "%Y" means a 4-digit year, e.g. 2004, however you could also use "%y" which means a 2-digit year, e.g. 04 represents 2004.
Example setting the x-axis range when date/time is used:
set xrange ["2005-02-01T00:00:00Z":"2005-04-01:T00:00:00Z"]
To set the widths of the major x-axis ticks when a time scale is used, for example:
set xtics "00:00:00","04:00:00","23:00:00"
This is for a plot with a time x-axis for the range 00:00 to 23:00, with major ticks every 4 fours.
To have a grid:
To include multiple plots in a single page, we use the multiplot command. For example, to have 3 rows with 2 columns each:
set multiplot layout 3,2 title "My 6 plots"
Note that if you are using a very old version of gnuplot you won't be able to specify a layout, and will have to manually specify the sizes and locations of each individual plot.
If you have trouble with multiple plots not lining up nicely, manually force margins. For example, to ensure the left margin of each plot is lined up vertically:
set lmargin 5
If you have several rows with different magnitudes of data, you can easily end up with plots not lining up vertically, so setting a left margin is very useful.
If you just have a single plot and are not using the multiplot command, a title can be specified like this:
set title "Time history of magnitude since event"
To plot data from an ASCII file data.txt using lines:
plot "data.txt" using 1:2 title "data 01" with lines
This will plot data using x values from column 1 and y values from column 2. Note that the first column is column 1, not 0. Replace "with lines" with "with points" to use points instead of lines.
To plot multiple sets of data from the same file:
plot "data.txt" using 1:2 title "data 01" with lines,\
"data.txt" using 1:3 title "data 02" with lines,\
"data.txt" using 1:4 title "data 03" with lines
If you want to exclude some data from a plot, for example negative values:
plot "data.txt" using 1:(($2 >= 0) ? $2 : 1/0) title "data (positive only)" with points
It's easy to include simple maths:
plot "data.txt" using 1:(2.5*$2/$3) title "values" with points
This will plot the ratio of column 2 to column 3 multiplied by 2.5.
The text specified by title in the plot command is used in the legend. To not show any legend:
set key off
To display a key in the bottom right of the plot, for example, use:
set key bottom right
To set x-axis and y-axis labels:
set xlabel "Time since event"
set ylabel "Magnitude"
To show a timestamp at the bottom of a plot:
If your x-axis major tick values take up too much space and overlap, you can rotate them:
set xtics nomirror rotate by -45
This will rotate them by -45 degrees.