Gnuplot

From PHYSpedia
Revision as of 23:02, 20 February 2013 by Cclark (talk | contribs)

Jump to: navigation, search

Gnuplot is a terminal based plotting application. This means that you interact with gnuplot by typing commands at a prompt. This turns out to be very convenient, because it means that you can always save or create a gnuplot configuration into a plain text file. Several good tutorial references exist for gnuplot.

Gnuplot Official Site (Tutorial/User Manual PDF): http://www.gnuplot.info/

Gnuplot Not So Frequently Asked Questions: http://t16web.lanl.gov/Kawano/gnuplot/index-e.html

The book, Gnuplot in Action: http://www.manning.com/janert/


Installation

If gnuplot is not already installed on your system, you need to install it. The method of install will depend on your OS and distribution. A few common examples are

Gentoo
# must run as root
> emerge gnuplot
Ubuntu
> sudo apt-get install gnuplot
Fedora
# must run as root
> yum install gnuplot
Debian
# must run as root
> apt-get install gnuplot
Windows
A windows install file can be downloaded from the gnuplot sourceforge page here.
Mac OS X
First, download XQuartz from here and install. Then download the gnuplot-4.6.0.tar.gz from here. Now you will just unpack, configure, build and install gnuplot from source.
 > tar xzf gnuplot-4.6.0.tar.gz
 > cd gnuplot-4.6.0
 > ./configure
 > make
 > make install     # run as root.
gnuplot can also be installed on Mac OS with MacPorts


Other
Of course, gnuplot can be built from source on any other Unix based machine such as Mac OS X and Linux.

Plotting

gnuplot is a program created to plot stuff. You can plot functions, data, or both. The plot command has the following form

gnuplot> plot function | 'filename' datafile-modifiers [, function2 | 'filename2' datafile-modifiers ]

Plotting Functions

To plot a function, just give the function to plot to the plot command.

gnuplot> plot sin(x)

Note that x is a special variable that gnuplot reconizes as the independent variable. Multiple functions can be plotted on the same graph, just separate each with a comma

gnuplot> plot sin(x), cos(x)

The default plot generated by gnuplot is not bad (the background isn't gray), but unless you are just quickly plotting something for your own use, you will need to change this. For example, you will want to label the axes and indicate their units. Gnuplot allows the way the plot is displayed to be configured by setting options. To set options, use the set command

gnuplot> set title "Displacement of an Oscillating Spring"
gnuplot> set xlabel "time (s)"
gnuplot> set ylabel "displacement (m)"

Gnuplot does not redraw the graph automatically. To update the plot, either reissue the plot command, or just use the replot command.

gnuplot> replot  # redraw the previous plot with new options

See this section for a list of common options.

User-Defined Functions and Variables

gnuplot allows the user to define arbitrary functions. Function definitions have the following form

gnuplot> function_name(arg1, [arg2, ...] ) = expression

User-defined functions can be plotted with the plot command.

gnuplot> gauss(x) = exp( - x**2 )
gnuplot> plot gaus(x)

Functions that take more than one argument can also be plotted. Just pass x to one of the arguments.

gnuplot> gauss(sigma, x0, x) = exp( - (x - x0)**2 / (2*sigma*sigma))
gnuplot> plot gauss(0.1, 5, x), gaus(0.2, 5, x)

Note that, while we used the variable x in the definition of gauss, it is not required. The function can be plotted for any one of the arguments

gnuplot> plot gauss(x, 5, 0)

gnuplot also also supports variables. You can set a variable, and then use it in plot commands or even function definitions.

gnuplot> x0 = 1.5
gnuplot> sigma = 0.75
gnuplot> gauss2(x) = exp( - (x - x0)**2 / (2*sigma*sigma))

This function will use the values stored in x0 and sigma when it is evaluated. Note that the x0 and sigma in the function gauss defined above will not use the variables. Function argument names will "hide" variable names. In other words, gnuplot variables have scope.

gnuplot> plot gauss(0.5, 1, x), gauss2(x)

Plotting Data from a File

We often need to plot data that is stored in a file. Typically this has either been generated from a computer model, or collected in an experiment. gnuplot has a very powerful set of utilities for plotting data from a file. The data files used in the examples that follow can be downloaded here.

simple.txt

gnuplot reads plain text files in table format: one row per line with columns separated by white space. Each row in the table is used to create one data point on the graph. The simplest file that gnuplot can read contains just two columns of data points,

> cat simple.txt
  0.0  1.1
  0.1  1.3
  0.2  1.4
  0.3  1.9

gnuplot can plot this file directly,

gnuplot> plot 'simple.txt'  # note the quote marks


This turns out to be fairly convenient because it is not very difficult to write code to write files in this format

 // C
 
 for(i = 0; i < n; i++)
   printf( fp, "%f %f\n", x[i], y[i] );
 
 // C++
 
 for(i = 0; i < n; i++)
   std::cout<<x[i]<<" "<<y[i]<<std::endl;

When analyzing data, this is often all that is required to see trends. However, if we are going to show out graph to anybody else, we will certainly want to fix it up.

gnuplot> set xlabel 'x (arbitrary)'  # arbitrary units on the x-axis
gnuplot> set ylabel 'y (arbitrary)'  # arbitrary units on the y-axis
gnuplot> set title 'Simple Graph'
gnuplot> set xrange[-.1:.4]          # widen xrange to see endpoints
gnuplot> set yrange[1:2]             # same for yrange
gnuplot> set key left                # move key to left, out of the way
gnuplot> replot                      # dont' forget to replot


gnuplot can display data points as points (as above), lines, or points connected by lines. This is controlled by setting the style option for data

gnuplot> set style data lines
gnuplot> rep                   # rep is shorthand for replot


gnuplot> set style data linespoints
gnuplot> rep
Choosing and Manipulating Data

The data we want to plot may be in a file with multiple columns. By default, gnuplot will read the file and use only the first two columns. The first column is used for the x coordinate, the second column is used for y.

> cat simple2.txt
0.0  1.1  2.4  5.4
0.1  1.3  2.7  6.3
0.2  1.4  2.9  7.5
0.3  1.9  3.1  9.1

Plotting this file will give the same graph as the previous file. However, to plot the 3rd column of data, we add the using modifier.

gnuplot> plot 'simple2.txt' using 1:3

The using modifier selects the columns to use in the plot. As expected, the above plot command selects the first and third columns.

using
select columns from

Plot Display Options

Gnuplot allows fine control over how plots are displayed. The plot display is controlled through options which are set with the set command. The set command takes an option name and its value. Some options can be unset with the unset command. Here is a short list of some common options

title
title of plot
gnuplot> set title "string"
xlabel, ylabel
labels for x and y axis
gnuplot> set xlabel "string"
gnuplot> set ylabel "string"
xrange, yrange
set the minimum and maximum values on the x and y axis. the range is specified in [min:max] format
gnuplot> set xrange [0:10] # set x axis range from 0 to 10
gnuplot> set yrange [-5:5] # set y axis range from -5 to 5
gnuplot> set xrange [:5] # set max on x-axis to 5. min is not changed
key
set the placement of the key/legend
gnuplot> set key left # move key to left of plot
gnuplot> set key right # move key to right of plot
gnuplot> set key top # move key to to top of plot
gnuplot> set key bottom # move key to to bottom of plot
gnuplot> unset key # remove key
style data
plot data points as points, lines, or points connected by lines (linespoints)
gnuplot> set style data points # plot data as points
gnuplot> set style data lines # plot data as lines
gnuplot> set style data linespoints # plot data as points connected by lines
terminal
set the output terminal (image file format)
gnuplot> set terminal png # write in png format
gnuplot> set terminal postscript # write in postscript format
gnuplot> set terminal jpeg # write in jpeg format
gnuplot> set terminal x11 # send plot to X11 (view plot on the screen)
output
set name of a file to write output to. this is used together with the terminal option.
gnuplot> set terminal png # write in png format
gnuplot> set terminal postscript # write in postscript format
gnuplot> set terminal jpeg # write in jpeg format
gnuplot> set terminal x11 # send plot to X11 (view plot on the screen)

Creating Images

gnuplot supports saving plots as an image in several different formats including png, jpg, postscript, and pdf. To save a plot as an image, first set the terminal option to the format you want to save the plot as, then set the output option to the name of the file.

gnuplot> set terminal png
gnuplot> set output "example.png"

Several different output "terminals" are supported, but different terminals are supported by different systems.

Gnuplot Scripts

The commands for creation of a gnuplot graph can be assembled as a text file and run as a gnuplot script by executing a command:

>gnuplot "script_file_name"

Gnuplot will execute the commands within the file. Here, the common use is to set the output terminal to postscript or some other format and viewed by an external viewer, like Okular.

A second method is to load a file from within gnuplot with the load command.

gnuplot> load "script_file-name"

Using this method, additional changes may be made to the graph with the interactive command line.

Graphical Interfaces

A number of graphical user interfaces exist for gnuplot. A common interface within Linux is grace. The grace package can typically be found along with gnuplot and installed in a similar fashion as described above.

Graphical interfaces are categorically frowned upon by this Department and its collaborators.

Other Reading