This is an old revision of the document!


Lab 05 - Plotting

Objectives

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

Contents

Gnuplot Introduction

Gnuplot is a free, command-driven, interactive, function and data plotting program. It can be downloaded at https://sourceforge.net/projects/gnuplot/. The official Gnuplot documentation can be found at http://gnuplot.sourceforge.net/documentation.html.

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 (http://gnuplot.sourceforge.net/docs_5.0/gnuplot.pdf).

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.

Tutorial

01. [10p] Basic Plotting

02. [10p] Fitting

03. [10p] Gnuplot Scripts

04. [10p] Animations

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 (https://en.wikipedia.org/wiki/Reduced_chi-squared_statistic), 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

Tasks

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] Gnuplot bar graphs

Use Gnuplot and the data from autoData.txt to generate separate 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.

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

Hint: Gnuplot conditional plotting.

07. [10p] Feedback

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

ep/labs/05.1572188684.txt.gz · Last modified: 2019/10/27 17:04 by emilian.radoi
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0