Skip to content

How To Start with 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.


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.

The second step is to set the line style, the colour, and finally the line type and line width.

The last step is to set the function and plot it.

and this is what we get :

sin(x) function with gnuplot



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 :

this gives the following output :

sin(x) cos(x)

sin(x) cos(x)

How to Plot Data:

To plot data from a file, the same principles are used

  1. Create a file.gnu    (This file contains the gnuplot commands) 
  2. 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 :

Here is how to generate the plot :

Once the output is set, we make up titles for the plot and the axis :

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 :

The legend is set at the bottom of the plot on the right side :

To generate an histogram this code is used :

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 with Error Bars :

Simply replace the last line by the following  (do not forget to add columns for the appropriate erros ) :

to generate the following plot :

error bars

error bars

Generate a heat map :

Create a data file using 3 columns :

set your title in your gnuplot file, your x and y axis legend and use the following code to generate the plot :

This will generate the following plot :

heat map

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”.

  1. Change the outer layout of the graph to the gray colour including the tics.
  2.  Change the colours of the line to pleasant colours, do not chose bright or dark colours.
  3. 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 :

and that is pretty much it.

 Tricks :

  •   is an online Gnuplot written in JS  made by Christian Huettig

 References :


{ 1 } Comments

  1. Fatal error: Uncaught Error: Call to undefined function ereg() in /home/users4/n/noktec/www/noktec/wp-content/themes/barthelme/functions.php:178 Stack trace: #0 /home/users4/n/noktec/www/noktec/wp-content/themes/barthelme/comments.php(34): barthelme_commenter_link() #1 /home/users4/n/noktec/www/noktec/wp-includes/comment-template.php(1554): require('/home/users4/n/...') #2 /home/users4/n/noktec/www/noktec/wp-content/themes/barthelme/single.php(44): comments_template() #3 /home/users4/n/noktec/www/noktec/wp-includes/template-loader.php(106): include('/home/users4/n/...') #4 /home/users4/n/noktec/www/noktec/wp-blog-header.php(19): require_once('/home/users4/n/...') #5 /home/users4/n/noktec/www/noktec/index.php(17): require('/home/users4/n/...') #6 {main} thrown in /home/users4/n/noktec/www/noktec/wp-content/themes/barthelme/functions.php on line 178