Banner: ZumGuy Publications and Network

ZumGuy Publications and Network

A simple gnuplot example

Posted by Sean on Sunday, 8th October 2017 10:16
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 samplegnuplotscript - 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 asmyplot.plt, for instance) on linux by runninggnuplot myplot.plt.

A brief explanation

Theset terminalcommand allows us to set the output format for our graphic. Changingpngtosvg, for instance, will generate an SVG file.
wxtwill 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 900x675 pixels size and the font to Verdana size 9.
set outputdetermines what the output file should be named.
set logscale xysets the x and y axes to logarithmic scale.
Theshowcommands simply give some feedback on the command line whengnuplotruns, they're not actually affecting the generated image.
set format xy "%g"will set the format of the axis tick labels. The%gformat specifier will select the shortest between floating-point notation (%f) and exponential notation (%e). Another useful format might be%P: multiples of Pi.
set xrangeandset yrangeallow us to set the range of the x and y axes. Leaving the range empty, as in ourxrangeexample, will useautoscale, and usingreversewill reverse the direction of the axis.
set gridwill display a grid on the graph.
set ylabelallows us to specify the title of the y axis, andset titledetermines the title of the entire plot.
Finally,plot "mypoints.dat" title "Method 1"takes the data from the filemypoints.datand 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 usetas 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:

gnuplot sample

You must be logged in to post messages.

Quote of the day...

ZumGuy Internet Promotions