Skip to content

How To Start with Gnuplot

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.

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

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 :

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

Histogram

 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

boring plot

While this is the amazing plot :

Amazing

Amazing Plot

 

And finally this is the code used to achieve the precedent plot :

and that is pretty much it.

 Tricks :

  • http://gnuplot.respawned.com/   is an online Gnuplot written in JS  made by Christian Huettig

 References :

  1. http://www.gnuplot.info/
  2. http://youinfinitesnake.blogspot.co.uk/
  3. http://gnuplot.sourceforge.net/demo/

{ 1 } Comments

  1. Jose | 21 December 2015 at 15 h 09 min | Permalink

    I saw your posting reinadgrg Gnuplot, and thought you might be interested to know that there is now a book on it: “Gnuplot in Action”. You can pre-order it directly from the publisher: .The book includes chapters on multi-dimensional plotting and color management, including recommendations for optimal palette design for data visualization.If you want to learn more about the book and the author, check out my book page at . Let me know if you are interested in a review copy.

Post a Comment

Your email is never published nor shared. Required fields are marked *