This is an old revision of the document!

Lab 05 - Plotting


  • Offer an introduction to Gnuplot
  • Get you familiarised with basic plots in Gnuplot

Gnuplot Introduction

Gnuplot is a free, command-driven, interactive, function and data plotting program. It can be downloaded at The official Gnuplot documentation can be found at

It was originally created to allow scientists and students to visualize mathematical functions and data interactively, but has grown to support many non-interactive uses such as web scripting. It is also used as a plotting engine by third-party applications such as Octave.

The command language of Gnuplot is case sensitive, i.e. commands and function names written in lowercase are not the same as those written in capitals. All command names may be abbreviated as long as the abbreviation is not ambiguous. Any number of commands may appear on a line, separated by semicolons (;). Strings may be set off by either single or double quotes, although there are some subtle differences. See syntax (p. 44) and quotes (p. 44) for more details in the Gnuplot documentation (

Commands may extend over several input lines by ending each line but the last with a backslash (\). The backslash must be the last character on each line. The effect is as if the backslash and newline were not there. That is, no white space is implied, nor is a comment terminated.

For built-in help on any topic, type help followed by the name of the topic or help ? to get a menu of available topics.


01. [10p] Basic Plotting

Download the following sets of data: data1.txt and data2.txt, and update gnuplot:

$ sudo apt-get install gnuplot

Start gnuplot by using the command:

$ gnuplot

The default terminal type is dependent on your environment. One of the recommended terminal types in terms of flexibility and functionality is wxt enhanced. To set the terminal type use:

gnuplot> set terminal wxt enhanced

If setting the terminal to wxt enhanced doesn't work, use the default terminal. If it is desired to display the terminal parameters at any point, use:

gnuplot> show terminal

Plot the sets of data found in data1.txt and data2.txt on the same graph:

gnuplot> plot 'data1.txt', 'data2.txt'

In case the window plot does not appear, you might be missing gnuplot-x11. Try installing it:

$ sudo apt-get install gnuplot-x11

Set a title for the plot:

gnuplot> set title 'Example 1'

Plot the data again for the title to appear. Notice that Gnuplot automatically selects different colours for each dataset. Change the colour for both datasets to black. This can be done with the lc parameter (stands for line colour). The command below changes the colour of the data from data2.txt to black.

gnuplot> plot 'data1.txt', 'data2.txt' lc rgb 'black'

The next step is to assign label names to the two axis:

gnuplot> set xlabel 'X Label'
gnuplot> set ylabel 'Y Label'
gnuplot> plot 'data1.txt' lc rgb 'black', 'data2.txt' lc rgb 'black

The top-right corner of the plot area displays the names of the files containing the data, along with the symbol type associated with each data file. This is usually not something you would want to have on a plot. To remove it use:

gnuplot> unset key
gnuplot> plot 'data1.txt' lc rgb 'black', 'data2.txt' lc rgb 'black

If you want to set it back you can use the set key command:

gnuplot> set key

The text of the keys can be changed when plotting the data like this:

gnuplot> plot 'data1.txt' lc rgb 'black' title 'Data 1', 'data2.txt' lc rgb 'black' title 'Data 2'

This is how the plot should look like so far:

It can be noticed that the data is obscuring the keys. In order to get the data off the keys, we can increase the Y axis to go all the way to 900. The X and Y ranges can be set as follows, using hard brackets and separating the low and high ranges by semicolon:

gnuplot> plot [:] [:900] 'data1.txt' lc rgb 'black' title 'Data 1', 'data2.txt' lc rgb 'black' title 'Data 2'

There is a gap showing on the right-hand side of the graph. This can be eliminated by setting the high range for the X axis to the number of rows in the data files which is 1024:

gnuplot> plot [:1024] [:900] 'data1.txt' lc rgb 'black' title 'Data 1', 'data2.txt' lc rgb 'black' title 'Data 2'

Saving the plot to an encapsulated postscript (eps is a vector graphic that can used in Latex documents) can be done as follows:

gnuplot> set terminal postscript eps enhanced “Helvetica” 24
gnuplot> set output 'exercise1.eps'
gnuplot> replot

Exit gnuplot, and notice that exercise1.eps was created. Open it and check how it looks.

02. [10p] Fitting

This exercise is aiming to familiarise you with using Gnuplot to do fitting. Start Gnuplot, change terminal to wxt enhanced and plot the data from data3.txt. The aim here is to fit a straight line between 800 and 1500. Plot the data from 800 to 1500 for a better look. This will give you the section that you will be trying to fit on a straight line (use w l (with line) to connect the points).

gnuplot> plot [800:1500] 'data3.txt'
gnuplot> plot [800:1500] 'data3.txt' w l

You need to specify the straight line:

gnuplot> f(x) = a + b*x

Fit the data between 800 and 1500 using the function that was just defined f(x), by varying the parameters a and b:

gnuplot> fit [800:1500] f(x) 'data3.txt' via a,b

The fit information shows the reduced chi-squared statistic which is used in the goodness of fit testing (, along with the slope parameter (b) with its uncertainty and the uncertainty percentage, the offset parameter (a) with the uncertainty in the offset and the uncertainty percentage, and the correlation matrix of the fit parameters.

Plot the fit on top of the initial data (use lw (line width) to set the thickness of the line):

gnuplot> plot [800:1500] 'data3.txt' w l, f(x) lw 3

Look again at the data from data3.txt, and fit a polynomical line between 0 and 1300:

gnuplot> f(x) = a+ b*x+ c*x*x
gnuplot> fit [0:1300] f(x) 'data3.txt' via a,b,c
gnuplot> plot [0:1300] 'data3.txt' w l, f(x) lw 3

03. [10p] Gnuplot Scripts

The benefit of using a script is that you do not have to retype everything every time you want to make a change or want to reproduce your plot. The following is an example of a Gnuplot script that plots two graphs on a single plot.

reset # flush all the variables

set size 1,1 # use default pallet size (100% of width and height)
set multiplot  

#Graph 1
set size 0.5,1 #half the width and full height 
set origin 0,0 #x,y 
plot 'data2.txt' w l lw 0.5

#Graph 2
set size 0.5,1 
set origin 0.5,0
plot 'data3.txt' w l lw 0.5

unset multiplot

Run the script:

$ gnuplot 
gnuplot> load 'script_name'

04. [10p] Animations

For very basic animations in Gnuplot you would need to set up a loop, decide what commands to give to Gnuplot and use the pipe utility to pipe that command into Gnuplot.

Example 1:

$ for ((i=-70; i<70; i++)); do echo -e "set sample 50000; set yrange [-40:40]; plot $i*sin(x)*cos(x) \n"; done | gnuplot

Example 2:

$ for ((i=-100; i<100; i++)); do echo -e "set isosample 100; spl [:] [:] [-100:100] $i*(sin(sqrt(x**2+y**2))/sqrt(x**2+y**2)) \n"; done | gnuplot -persist


05. [20p] Gnuplot graphs

Using the Gnuplot documentation, implement a script that plots four graphs. Use the data from data4.txt as follows: the first graph should plot columns 1 and 2, the second columns 1 and 3, the third one columns 1 and 4, and the fourth one should be a 3D graph plotting columns 1, 2 and 3.

  • Use different colours for the data in each graph.
  • Remove the keys for each graph.
  • Give a title to each graph.
  • Give names to each of the axes: X or Y (or Z for the 3D graph). In the case of the 3D graph: Make the numbers on the axes readable, and correct the position of the names of the axes if these are displayed over the axes numbers.
  • Make the script generate the .eps file containing your plot.

Hint: Consider the following commands: set key, xlabel, set title, set xtics, set terminal, set output, using.

06. [30p] Title_here2

Use Gnuplot and the data from autoData.txt to generate seperate bar graphs for the following:

  • The ”MidPrice” of all the ”small” cars.
  • The average fuel consumption (MPG - miles per gallon) for all the ”large” cars.
  • The ”MaxPrice” over the average fuel consumption for all ”chevrolet” and ”ford” cars.
  • Hint: Gnuplot conditional plotting.

The graphs should be as complete as possible (title, axes names, etc.)

07. [10p] Feedback

Please take a minute to fill in the feedback form for this lab.

ep/labs/05.1569223883.txt.gz · Last modified: 2019/09/23 10:31 by adriana.cogean
CC Attribution-Share Alike 3.0 Unported Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0