A simple gnuplot example

gnuplot is a very easy-to-use tool allowing us to quickly plot data we generated with, for instance, a C++ program.
Here is a sample gnuplot script – see a brief explanation of the commands below:

set terminal png size 900,675 enhanced font 'Verdana,9';
set output 'error_plot.png';

set logscale xy;
show logscale;

set format xy "%g";

set xrange [] reverse;
show xrange;

set yrange [1e-20:1];
show yrange;

set grid;

set ylabel "Error";
set xlabel "h";

set title "Error of derivative estimation";

plot "mypoints.dat" title "Method 1", "mypoints2.dat" title "Method 2";


You can execute this script (saved as myplot.plt, for instance) on linux by running gnuplot myplot.plt.

A brief explanation

The set terminal command allows us to set the output format for our graphic. Changing png to svg, for instance, will generate an SVG file. wxt will open a window showing the image instead of saving it to a file. The rest of the first line, size 900,675 enhanced font 'Verdana,9' sets the size of the output file to 900×675 pixels size and the font to Verdana size 9.
set output determines what the output file should be named.
set logscale xy sets the x and y axes to logarithmic scale.
The show commands simply give some feedback on the command line when gnuplot runs, they’re not actually affecting the generated image.
set format xy "%g" will set the format of the axis tick labels. The %g format specifier will select the shortest between floating-point notation (%f) and exponential notation (%e). Another useful format might be %P: multiples of Pi.
set xrange and set yrange allow us to set the range of the x and y axes. Leaving the range empty, as in our xrange example, will use autoscale, and using reverse will reverse the direction of the axis.
set grid will display a grid on the graph.
set ylabel allows us to specify the title of the y axis, and set title determines the title of the entire plot.
Finally, plot "mypoints.dat" title "Method 1" takes the data from the file mypoints.dat and plots it on our image. The title specified along with this command will appear in the legend. We can plot multiple sets of data on the same image by separating the files with a comma. The data files are text files formatted into two columns (I use t as a separator), with the first column representing the x values, and the other the y values. Here is an example:

1   0.48590043662431
0.1 0.0471667599770074
0.01    0.0046661958607152
0.001   0.000466079897104712
0.0001  4.66025581891169e-05
1e-05   4.6602014690932e-06
1e-06   4.66019603376477e-07
1e-07   4.66019549023389e-08
1e-08   4.66019543587199e-09
1e-09   4.66019542996309e-10
1e-10   4.66019543104729e-11
1e-11   4.66019540665275e-12
1e-12   4.66019556928307e-13
1e-13   4.66019909294013e-14
1e-14   4.66022619799444e-15
1e-15   4.66071408897206e-16
1e-16   4.65935883625646e-17
1e-17   4.63496428737553e-18
1e-18   5.14996031930615e-19
1e-19   0
1e-20   0


Here is what the above gnuplot script produces, given some data: