Difference between revisions of "Gnuplot"

From PHYSpedia
Jump to: navigation, search
(user-defined function)
(user-defined function)
Line 96: Line 96:
 
In the example above, we defined a function <code>f(x)</code> 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,
 
In the example above, we defined a function <code>f(x)</code> 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))
+
  gnuplot> gauss(sigma, x0, x) = exp( - (x - x0)**2 / (2*sigma*sigma))
  
 
To plot this function for different values of <code>x0</code> and <code>sigma</code>, we just provide values directly in the plot command
 
To plot this function for different values of <code>x0</code> and <code>sigma</code>, we just provide values directly in the plot command
  
  gnuplot> plot Gauss(.5,0,x), Gauss(0.5,1,x)
+
  gnuplot> plot gauss(.5,0,x), Gauss(0.5,1,x)
 +
 
 +
[[file:gnuplot_tutorial_pic_007.png]]
  
 
====plotting data from a file====
 
====plotting data from a file====

Revision as of 19:24, 13 March 2012

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

basic plotting

The plot command has the following form

gnuplot> plot [ function | 'filename' ] [ 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 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 tutorial pic 005.png

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 function

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(.5,0,x), Gauss(0.5,1,x)

Gnuplot tutorial pic 007.png

plotting data from a file

We often need to plot data that is stored in a file. 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.

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
  ... 
modifiers
using
select columns from

Creating Pictures

Gnuplot Scripts