Gnuplot:
Gnuplot is a “simple” command line tool that generates 2D, 3D, GIF graphs which can be used along with all major operating systems and which is often used in publications due to it’s high quality graphics. The next paragraphs illustrate a few simple examples to start as well as how and why to make attractive plots.
Examples:
Starting with gnuplot is not always as easy as it appears, many examples do not work, or use deprecated and “magic” to create graphs without any comments in the files and this might be disconcerting at first but everything comes with practice and patience.
To start open a text file in your favorite text editor (vim, sublime text, etc)
How to Plot a Function:
To start plotting it is important to specify the output terminal, which means how the plot will be displayed. In this case the plot will be displayed as a PNG image, some other output such as SVG, PDF, POSTSCRIPT, EPSLATEX can also be used.
|
#specify the the output type set terminal png #specify the name of my file set output 'function.png' |
The second step is to set the line style, the colour, and finally the line type and line width.
|
set style line 1 linecolor rgb '#0060ad' linetype 2 linewidth 2 |
The last step is to set the function and plot it.
|
function = 0.9 f(x) = function * sin(x) plot f(x) title 'sin(x)' with lines linestyle 1 |
and this is what we get :

sin(x)
As we can see gnuplot as generated our sin(x) in blue as specified before. It also possible to plot two function on the same graph by modifying our file to :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#set output set terminal png set output 'function.png' #set linestyle set style line 1 linecolor rgb '#0060ad' linetype 1 linewidth 2 set style line 2 linecolor rgb '#EE0000' linetype 1 linewidth 2 #set variables a = 0.9 b = 0.3 f(x) = a * sin(x) g(x) = b * cos(x) #plot plot f(x) title 'sin(x)' with lines linestyle 1, \ g(x) title 'cos(x)' with lines linestyle 2 |
this gives the following output :

sin(x) cos(x)
How to Plot Data:
To plot data from a file, the same principles are used
- Create a file.gnu (This file contains the gnuplot commands)
- Create a file.dat (This file contains the data)
Histogram with string as XTICS :
This plots ramdom data, strings are used as TICS and the output as set a PNG below you can find the data used :
|
Data Car1 Car2 Data0 2.4 2.5 Data1 3 5.5 Data2 9 60 |
Here is how to generate the plot :
|
#The output is set set terminal png set output 'data.png' |
Once the output is set, we make up titles for the plot and the axis :
|
#Place the title and the axis caption set title "Example" set xlabel "X label" set ylabel "Y label" |
The data are quiet different in term of size, a logarithmique scale is used for the plot, the range is also defined as well as the tics on the Y axis :
|
#using a log scale set logscale y #set our range set yrange[1:200.000000] #set tics on the graph set ytics (1.000000,10.000000,50,200.000000) nomirror |
The legend is set at the bottom of the plot on the right side :
|
#set the keys in the bottom set key right bottom |
To generate an histogram this code is used :
|
#plot set style fill solid border set style data histogram set style histogram clustered plot for [COL=2:3] "data.dat" using COL:xticlabels(1) title columnheader |
As shown, it is possible that COL:xticlabels(1) corresponds to the first column, therefor column XTIC has Data0 as name and this is the output generated.

Histogram
Histogram with Error Bars :
Simply replace the last line by the following (do not forget to add columns for the appropriate erros ) :
|
set style histogram errorbars gap 2 lw 1 plot "data.dat" using 2:5:xtic(1) title columnheader ,"" u 3:4:xtic(1) t columnheader |
to generate the following plot :

error bars
Generate a heat map :
Create a data file using 3 columns :
|
0 0 0 1 1 1 2 2 2 3 3 3 ... 999 999 999 |
set your title in your gnuplot file, your x and y axis legend and use the following code to generate the plot :
|
# start value (HSV Values from GIMP) X1 = 117/360.0 # end value X2 = 227/360.0 # creating the palette by specifying H,S,V set palette model HSV functions (1-gray)*(X2-X1)+X1,1,0.7 plot "data.dat" using 1:2:3 title columnheader w l lc palette |
This will generate the following plot :

heat map
Attractive plots:
Even though the plots generated with gnuplot have a “good quality” it is possible to enhance the output by using simple tweaks and the ‘cairo’ library, to go a bit further, I would suggest to avoid underestimating the power of a “sexy” plot ! (and this takes us back to Social Engineering).
Why would people create sexy plots ? it’s a plot after all ! simply because plots are catching the eyes much faster than a long text, and that it is easier and faster for the reader to look at a plot and identify the information contained.
And as someone once said : “A picture is worth a thousand words”.
This section is based on Brighten Godfrey’s post So, in this section I will only make a simple summary of the steps to follow a a simple comparisons of two plots generated with “pngcairo”.
- Change the outer layout of the graph to the gray colour including the tics.
- Change the colours of the line to pleasant colours, do not chose bright or dark colours.
- Add a grid to the background, to allow the reader to analyse the data as fast as possible.
Here is the first boring plot :

boring plot
While this is the amazing plot :

Amazing Plot
And finally this is the code used to achieve the precedent plot :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
set terminal pngcairo set output 'boring.png' #Place the title and the axis caption set title "Example" set xlabel "X label" set ylabel "Y label" # Style of the lines set style line 80 lt rgb "#808080" # Grid lines in grat set style line 81 lt 0 set style line 81 lt rgb "#808080" #set style + no mirror set grid back linestyle 81 set border 3 back linestyle 80 set xtics nomirror set ytics nomirror #Chose simple colours as mentioned in Brighten Godfrey<b>'s blog post.</b> set style line 1 lt rgb "#A00000" lw 2 pt 1 set style line 2 lt rgb "#00A000" lw 2 pt 6 #plot the data plot 'data.dat' u 1:2 t 'Example line' w lp ls 1, \ '' u 1:3 t 'Another example' w lp ls 2 |
and that is pretty much it.
Tricks :
- http://gnuplot.respawned.com/ is an online Gnuplot written in JS made by Christian Huettig
References :
- http://www.gnuplot.info/
- http://youinfinitesnake.blogspot.co.uk/
- http://gnuplot.sourceforge.net/demo/
Tagged cairo, errorbars, gnuplot, heatmap, histogram, online, plots