As part of an investigation project at work, we had to create a number of graphs. Of course our first idea was using Excel; but it turns out that in a lot of scenarios it’s ambiguous, time consuming and sometimes outright frustrating. So we ended up doing it with Gnuplot, which provided a much better experience.
This article is not meant to give extended coverage of course; there are many FAQs and other documents available online for that (a small collection is given at the end of the article). It’s meant to cover basic usage and some common scenarios, namely:
- How to download and install
- How to plot a simple function
- How to plot data points from a file
- How to plot multiple functions and/or data points
- How to setup the plot (axes etc.)
- How to fill the area between functions
- How to export the plot for MS Office
- How to plot using batch files
- Links and FAQs
Gnuplot is a really powerful tool. This article won’t cover many things, like 3D plots, polar coordinates, binary data, financial-style graphs and others; take a look at the demo library for that (link given at the end).
How to download and install
Download is provided from Sourcefourge. Go to http://www.gnuplot.info/download.html and get the current version.
After downloading, installation is pretty easy and straightforward. Just click “next” in every step and you’ll be ok.
After installation, start Gnuplot from the desktop icon. You’ll get a command prompt (gnuplot>).
How to plot a simple function
A major problem with MS Excel is that you cannot create a graph for a function; you have to create the data in cells, using a formula. And of course, the values will not be continuous but discrete.
So let’s say you want to make a graph of a function f(x)=x^2+10/x, for values of x between -10 and 10. Enter these commands to the command prompt, pressing ENTER after each line (lines that begin with # are comments):
# setup the x axis range set xrange [-10:10] # plot our function f(x)=x**2+10/x plot f(x)
To change the line color, the easiest way is to use one of the available linestyles:
In order to see the readily available linestyles, just enter:
How to plot data points from a file
For our example, we have two text tab-separated files, c:\temp\out1.txt and c:\temp\out2.txt, that look like this:
The first has just two columns, x and y. The second has four columns: the second is labels on x, the third and fourth are measurement values (y) and the first an incremental number (for gnuplot to know which comes first, second etc.)
Let’s plot the first one:
Note the 1:2 here. This tells gnuplot that the 1st column of the file will be used for x and the 2nd for y.
If you want to connect the points, the last line would be:
If instead of simply connecting the points you would need to do a ‘best fit’ with a given function, say g(x)=a*x+c :
Of course, that’s not a very accurate fit, but that’s not our point here
Let’s now plot the second file. Our goal here is to create a bar chart:
Note the 1:3:xtic(2). This tells gnuplot that the 1st column is ot be used for x, the 3rd for y and the 2nd (xtic(2)) for x-axis labels.
Now let’s try to plot two data series in the same bar chart:
How to plot multiple functions and/or data points
Actually we did that already in the example with the fit and bar examples. We just have to give multiple functions/files and separate them with a comma. As an example:
Let’s add a line and a legend, shall we ? The last line will become:
How to setup the plot (axes etc.)
How to fill the area between functions
What if you want to fill the area below a curve, or between two curves ?
First, let’s fill the area between a curve f(x)=x^2 and the x axis:
After this, it should be obvious how you can fill the area between two curves; just use a function instead of c(x)=0. Let’s say we use g(x)=x^1.8
How to export the plot for MS Office
Although of course you can do a printscreen, the best format to use with Word, Powerpoint etc. is the Enhanced Metafile Format (.emf). The best thing about it is that it’s scalable. Surprisingly, if you have a .emf image and preview it (Windows uses Paint by default) it seems awful; but if you insert it in Word it looks great.
So, in order to create a plot and get it as an .emf you need to do something like this:
How to plot using batch files
Creating a batch file is useful in several scenarios. For example, you might have a data file (like out1.txt above) which changes every day; and every day you need to create the same graph, but with the fresh data.
So in order to do this, just write all gnuplot commands to a text file and execute it with gnuplot.exe. See the example below (the backslash means that the line is continued):
The .plt extension is the gnuplot default, but you can name the file anything (e.g. .txt). Now open a Windows command prompt and type:
Links and FAQs
By far the best resource is the demo scripts library. Here you can find almost anything and adapt it for your scenario.
Demo scripts for gnuplot (with images of output)
gnuplot Quick Reference (.pdf)
A Brief Manual and Tutorial
Official gnuplot documentation