Difference between revisions of "Gnuplot"

From PHYSpedia
Jump to: navigation, search
(Other Reading)
(Other Reading)
Line 253: Line 253:
 
* [http://gnuplot-surprising.blogspot.com/ gnuplot surprising]: a blog of gnuplot examples and tips
 
* [http://gnuplot-surprising.blogspot.com/ gnuplot surprising]: a blog of gnuplot examples and tips
 
* [http://linuxgazette.net/168/misc/lg/2_cent_tip__piping_to_gnu_plot_from_c.html Linux Gazette]: piping from C to gnuplot
 
* [http://linuxgazette.net/168/misc/lg/2_cent_tip__piping_to_gnu_plot_from_c.html Linux Gazette]: piping from C to gnuplot
* [http://gnuplot-tricks.blogspot.com/2009/05/gnuplot-tricks-many-say-that-it-is.html Gnuplot Tips]: Make publication quality plots with EPS terminal
+
* [http://gnuplot-tricks.blogspot.com/2009/05/gnuplot-tricks-many-say-that-it-is.html Gnuplot Tips Blog]: Make publication quality plots with EPS terminal and include symbols

Revision as of 17:12, 22 January 2013

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.

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.


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

Getting Started

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/

Plotting

gnuplot is a program created to plot stuff. You can plot functions or data. There are two commands for plotting, plot and splot. plot is used to plot 2D functions while splot is used to plot 3D functions.

Plotting Functions

The plot command has the following form

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

gnuplot has great support for all common (and many uncommon) functions. For example, to plot sin(x),

gnuplot> plot sin(x)

Gnuplot tutorial pic 001.png

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

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

Gnuplot tutorial pic 002.png

We control the what and how our plot is displayed by setting options. Options are set with the set command. For example, to set the graph title, we set the title option to a string containing the title.

gnuplot> set title "Sine and Cosine"

Notice that the graph does not change after you set the title option. gnuplot will not redraw the graph until we either issue another plot command, or tell it to replot what we previously plotted with the ...replot command.

gnuplot> replot  # redraw the previous plot with new options

Gnuplot tutorial pic 003.png

Other important options include xlabel (the x-axis label), ylabel (the y-axis label) and key (sets the position of the legend). The legend is located in the top-right corner of the graph by default, which is in the way in the example above. To label the axis and move the key,

gnuplot> set xlabel "time (s)"
gnuplot> set ylabel "current (A)"
gnuplot> set key top left
gnuplot> replot

Gnuplot tutorial pic 004.png

By default, gnuplot will "autoscale" the y axis (when plotting data from a file, the x axis is autoscaled too). To set the x and y axis ranges, set the xrange and yrange options.

gnuplot> xrange[0:10]
gnuplot> yrange[-2:2]
gnuplot> replot

Gnuplot tutorial pic 005.png

You can also set the range directly in the plot command.

gnuplot> plot [x=0:10] sin(x)

Autoscaling can always be re-enabled by setting the autoscale option

gnuplot> set autoscale

or, to enable autoscaling for the x or y axis individually,

gnuplot> set autoscale x   # autoscale the x axis
gnuplot> set autoscale y   # autoscale the y axis

gnuplot allows aritrarly complex functions to be plotted. These can either be built directly in the plot command,

gnuplot> plot sin(x) + cos(2*x) + 3*sin(4*x)

or you can define a function and plot it

gnuplot> f(x) = sin(x) + cos(2*x) + 3*sin(4*x)
gnuplot> plot f(x)

Gnuplot tutorial pic 006.png

user-defined functions

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

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

In the example above, we defined a function f(x) that was a function of only one variable. However, we can define function of multiple variables. This can be very handy if we would like to plot a function with a variable parameter. For example, we may want to plot a Gaussian, but allow the center and width to be changed. We could define a Gauss function,

gnuplot> gauss(sigma, x0, x) = exp( - (x - x0)**2 / (2*sigma*sigma))

To plot this function for different values of x0 and sigma, we just provide values directly in the plot command

gnuplot> plot gauss(0.5,0,x), gauss(0.5,1,x), gausss(1.0,1,x)

Gnuplot tutorial pic 007.png

Note that, while we used the variable x<\code> in the definition of <code>gauss, it is not required. More importantly, we can plot a function agains any of its arguments.

gnuplot> plot gauss(0.5,x,1)

Gnuplot tutorial pic 008.png

gnuplot also also supports variables. You can set a variable, and then use it in plot commands or even function definitions. For example, we could set the width and position of a Gaussian function through two variables.

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 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)

Gnuplot tutorial pic 009.png

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

Gnuplot tutorial pic 010.png

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 tutorial pic 011.png

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

lines vs. points

By default, gnuplot will plot data as points. To change this, we can set the data style to lines or linespoints (actually, other styles exist, but are much less common).

Creating Pictures

gnuplot separates the concept of creating a plot and outputting a plot. Basically, all of the work required to construct a plot (reading in data points from a file, evaluating functions, etc) is independent of how the plot will be displayed. For example, you may want to the plot to be displayed on the screen, or written to a jpg. gnuplot uses the idea of terminals to support outputting plots to multiple formats. To output a plot to a jpeg, you tell gnuplot to write to the jpeg terminal type. To output a plot a png, you tell gnuplot to write to the png terminal type. You get the idea.

To change the terminal type, set the terminal option. For example, to create a png, set the terminal type to png.

gnuplot> set terminal png

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