Difference between revisions of "Gnuplot"

From PHYSpedia
Jump to: navigation, search
(Installation)
(Plot Display Options)
 
(47 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
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.
 
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/ http://www.gnuplot.info/]
 +
 +
Gnuplot Not So Frequently Asked Questions: [http://t16web.lanl.gov/Kawano/gnuplot/index-e.html http://t16web.lanl.gov/Kawano/gnuplot/index-e.html]
 +
 +
The book, Gnuplot in Action: [http://www.manning.com/janert/ http://www.manning.com/janert/]
 +
 +
 
==Installation==
 
==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
 
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
Line 19: Line 28:
  
 
;Windows
 
;Windows
: A windows install file can be downloaded from the gnuplot sourceforge page [http://sourceforge.net/projects/gnuplot/files/gnuplot/4.6.0/ here]
+
: A windows install file can be downloaded from the gnuplot sourceforge page [http://sourceforge.net/projects/gnuplot/files/gnuplot/4.6.0/ here].
  
 
;Mac OS X
 
;Mac OS X
: First, download XQuartz from [http://xquartz.macosforge.org/landing/ here] and install. Then download the <code>gnuplot-4.6.0.tar.gz</code> from [http://sourceforge.net/projects/gnuplot/files/gnuplot/4.6.0/ here].  
+
: First, download XQuartz from [http://xquartz.macosforge.org/landing/ here] and install. Then download the <code>gnuplot-4.6.0.tar.gz</code> from [http://sourceforge.net/projects/gnuplot/files/gnuplot/4.6.0/ 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
 
;Other
Line 28: Line 44:
  
 
==Plotting==
 
==Plotting==
gnuplot is a program created to plot stuff. You can plot functions or data. There are two commands for plotting, <code>plot</code> and <code>splot</code>. <code>plot</code> is used to plot 2D functions while <code>splot</code> is used to plot 3D functions.  
+
gnuplot is a program created to plot stuff. You can plot functions, data, or both. The <code>plot</code> command has the following form
 
 
===plotting functions===
 
The <code>plot</code> command has the following form
 
  
  gnuplot> plot [ function | 'filename' datafile-modifiers ]
+
  gnuplot> plot function | 'filename' datafile-modifiers [, function2 | 'filename2' datafile-modifiers ]
  
gnuplot has great support for all common (and many uncommon) functions. For example, to plot sin(x),
+
===Plotting Functions===
 +
To plot a function, just give the function to plot to the <code>plot</code> command.
  
 
  gnuplot> plot sin(x)
 
  gnuplot> plot sin(x)
 +
Note that <code>x</code> is a special variable that gnuplot reconizes as the independent variable in a <code>plot</code> command.
  
[[File:gnuplot_tutorial_pic_001.png|400px]]
+
Multiple functions can be plotted on the same graph, just separate each with a comma
 
 
Note that <code>x</code> 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> plot sin(x), cos(x)
  
[[File:gnuplot_tutorial_pic_002.png|400px]]
+
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. Options are set with the <code>set</code> command
  
We control the what and how our plot is displayed by setting ''options''. Options are set with the <code>set</code> command. For example, to set the graph title, we set the <code>title</code> option to a string containing the title.
+
gnuplot> set title "Displacement of an Oscillating Spring"
 
+
gnuplot> set xlabel "time (s)"
  gnuplot> set title "Sine and Cosine"
+
  gnuplot> set ylabel "displacement (m)"
  
Notice that the graph does not change after you set the title option. gnuplot will not redraw the graph until we either issue another <code>plot</code> command, or tell it to ''replot'' what we previously plotted with the ...<code>replot</code> command.
+
Gnuplot does not redraw the graph automatically after options are set. To update the plot, either reissue the same <code>plot</code> command, or just use the <code>replot</code> command.
  
 
  gnuplot> replot  # redraw the previous plot with new options
 
  gnuplot> replot  # redraw the previous plot with new options
  
[[File:gnuplot_tutorial_pic_003.png|400px]]
+
See the [[#Plot Display Options]] section for a list of common options.
  
Other important options include <code>xlabel</code> (the x-axis label), <code>ylabel</code> (the y-axis label) and <code>key</code> (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,
+
====User-Defined Functions and Variables====
 
 
gnuplot> set xlabel "time (s)"
 
gnuplot> set ylabel "current (A)"
 
gnuplot> set key top left
 
gnuplot> replot
 
 
 
[[File:gnuplot_tutorial_pic_004.png|400px]]
 
 
 
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 <code>xrange</code> and <code>yrange</code> options.
 
 
 
gnuplot> xrange[0:10]
 
gnuplot> yrange[-2:2]
 
gnuplot> replot
 
 
 
[[File:gnuplot_tutorial_pic_005.png|400px]]
 
 
 
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 <code>autoscale</code> 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 <code>plot</code> 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)
 
 
 
[[File:gnuplot_tutorial_pic_006.png|400px]]
 
 
 
====user-defined functions====
 
 
gnuplot allows the user to define arbitrary functions. Function definitions have the following form
 
gnuplot allows the user to define arbitrary functions. Function definitions have the following form
  
 
  gnuplot> function_name(arg1, [arg2, ...] ) = expression
 
  gnuplot> function_name(arg1, [arg2, ...] ) = expression
  
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,
+
User-defined functions can be plotted with the plot command.
 
 
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
+
gnuplot> gauss(x) = exp( - x**2 )
 +
gnuplot> plot gaus(x)
  
gnuplot> plot gauss(0.5,0,x), gauss(0.5,1,x), gausss(1.0,1,x)
+
Functions that take more than one argument can also be plotted. Just pass <code>x</code> to one of the arguments.
  
[[file:gnuplot_tutorial_pic_007.png|400px]]
+
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 <code>x<\code> in the definition of <code>gauss</code>, it is not required. More importantly, we can plot a function agains ''any'' of its arguments.
 
  
gnuplot> plot gauss(0.5,x,1)
+
Note that, while we used the variable <code>x</code> in the definition of <code>gauss</code>, it is not required. The function can be plotted for any one of the arguments
  
[[file:gnuplot_tutorial_pic_008.png|400px]]
+
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. For example, we could set the width and position of a Gaussian function through two variables.
+
gnuplot also also supports variables. You can set a variable, and then use it in <code>plot</code> commands or even function definitions.
  
 
  gnuplot> x0 = 1.5
 
  gnuplot> x0 = 1.5
Line 125: Line 96:
 
  gnuplot> gauss2(x) = exp( - (x - x0)**2 / (2*sigma*sigma))
 
  gnuplot> gauss2(x) = exp( - (x - x0)**2 / (2*sigma*sigma))
  
This function will use the values stored in <code>x0</code> and <code>sigma</code> when it is evaluated. Note that the <code>x0</code> and <code>sigma</code> in the function <code>gauss</code> will '''not''' use the variables. Function argument names will "hide" variable names. In other words, gnuplot variables have ''scope''.
+
This function will use the values stored in <code>x0</code> and <code>sigma</code> when it is evaluated. Note that the <code>x0</code> and <code>sigma</code> in the function <code>gauss</code> 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), gauss2(x/sigma)
  
gnuplot> plot gauss(0.5, 1, x), gauss2(x)
+
===Plotting Data from a File===
  
[[file:gnuplot_tutorial_pic_009.png|400px]]
+
gnuplot is very useful for plotting arbitrary functions, but it really shines when it comes to plotting data in a file. gnuplot makes it simple to quickly plot data stored in a plain text file. Typically this has either been generated from a computer model, or collected in an experiment. Here are two small text files containing data that can be used to follow the examples below.
  
===plotting data from a file===
+
[[Media:simple.txt|simple.txt]]
  
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.
+
[[Media:simple2.txt|simple2.txt]]
  
[[Media:simple.txt|simple.txt]]
+
[[Media:large-data.txt|large-data.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,
 
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,
Line 144: Line 117:
 
   0.3  1.9
 
   0.3  1.9
  
gnuplot can plot this file directly,
+
gnuplot can plot this file directly. The first column is used for the x coordinates and the second column is used for the y coordinates,
  
  gnuplot> plot 'simple.txt'  # note the quote marks
+
  gnuplot> plot 'simple.txt'  # note the quotes. single or double quotes can be used
  
[[file:gnuplot_tutorial_pic_010.png|400px]]
+
This turns out to be very convenient because this is about the simplest format to write data to from within a program
  
This turns out to be fairly convenient because it is not very difficult to write code to write files in this format
+
  // arrays x and y contain the x and y coordinates of our data
  
 
   // C
 
   // C
Line 162: Line 135:
 
     std::cout<<x[i]<<" "<<y[i]<<std::endl;
 
     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.
+
=====More Sophisticated Data Files=====
 +
gnuplot can plot data from files even if there are more than two columns. By default, gnuplot will read the file and use the first two columns and ignore the rest.
 +
 +
> 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 <code>using</code> modifier.
 +
 
 +
gnuplot> plot 'simple2.txt' using 1:3
 +
 
 +
The <code>using</code> modifier selects the columns to use in the plot, but can also be used to manipulate the data ''before'' it is plotted. For example, to multiply all y values by 4.5 and plotting the results,
 +
 
 +
gnuplot> plot 'simple2.txt' using 1:(4.5*$3)
 +
 
 +
Notice the parenthesis and the $3. If an integer is given, the integer is taken to be the column number to use. If a calculation involving columns is to be used, it must be enclosed with parenthesis and the columns are refered to with a '$' (similar to gawk). Calculations involving columns can use data from ''any'' column. The using command can be used to do some ''very'' powerful things
 +
 
 +
gnuplot> plot 'simple2.txt' using 1:($2*$1)                          # plot column 1 times column 2 vs column 1
 +
gnuplot> plot 'simple2.txt', 'simple2.txt' using 1:(1.5 * $1 + 1.1)  # plot column 2 vs column 1 and and the line 1.5x + 1.1 using x values from column 1
 +
gnuplot> plot 'simple2.txt' using ($1 - 10):2                        # plot column 2 vs column 1, but shift the x axis by 10
 +
gnuplot> plot 'simple2.txt' using (2*$1):(5*exp( $2 ) )              # plot column 2 vs column 1, manipulating both first
 +
 
 +
Sometimes, it may be useful to only plot some of the points in a data file. For example, if the data file is very large, it can take a while for gnuplot to generate the plot. By plotting only every other point, or every 10th point, the plot time can be shortened. To limit the number of points that are plotted from a data file, use the <code>every</code> modifier.
 +
 
 +
gnuplot> plot 'simple2.txt' every 2                          # plot every other line in simple2.txt using columns 1 and 2
 +
gnuplot> plot 'simple2.txt' every 3 using 1:3                # plot every 3rd line in simple2.txt using columns 1 and 3
 +
 
 +
===<code>plot</code> Command Modifiers===
 +
In its simplest form the plot command just takes a list of functions or files to plot. However, each plot can be ''modified'' by specifying a set of modifiers. The modifiers are just given after the function or file to be plotted. For example, <code>using</code> and <code>every</code> are modifiers that can be applied to data files.
 +
 
 +
;<code>using</code>
 +
: select which columns in a data file to plot
 +
gnuplot> plot 'data.txt' using 1:3
 +
 
 +
;<code>every</code>
 +
: only plot some lines from a data file
 +
gnuplot> plot 'data.txt' every 2
 +
 
 +
;<code>title</code>
 +
: set the string used for a plot in the key/legend
 +
gnuplot> plot 'data.txt' title "raw data", '' using 1:(2*$2) title "processed data", x*x 'quadradic'
  
gnuplot> set xlabel 'x (arbitrary)' # arbitrary units on the x-axis
+
===Plot Display Options===
gnuplot> set ylabel 'y (arbitrary)' # arbitrary units on the y-axis
+
Gnuplot allows fine control over how plots are displayed. The plot display is controlled through options which are set with the <code>set</code> command. The <code>set</code> command takes an option name and its value.
gnuplot> set title 'Simple Graph'
+
Some options can be ''unset'' with the <code>unset</code> command.
gnuplot> set xrange[-.1:.4]          # widen xrange to see endpoints
+
Here is a short list of some common options
gnuplot> set yrange[1:2]            # same for yrange
+
; title
gnuplot> set key left                # move key to left, out of the way
+
: title of plot
gnuplot> replot                      # dont' forget to replot
+
  gnuplot> set title "string"
  
 +
; xlabel, ylabel
 +
: labels for x and y axis
 +
  gnuplot> set xlabel "string"
 +
  gnuplot> set ylabel "string"
  
[[file:gnuplot_tutorial_pic_011.png|400px]]
+
; 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
  
gnuplot can display data points as points (as above), lines, or points connected by lines. This is controlled by setting the <code>style</code> option for <code>data</code>
+
; xtics, ytics
 +
: set values marked on the x or y axis.
 +
  gnuplot> set xtics 1  # mark every whole number on the x axis
 +
  gnuplot> set ytics 2  # mark every even number on the y axis
  
gnuplot> set style data lines
+
; grid
gnuplot> rep                  # rep is shorthand for replot
+
: draw a grid on the plot canvas.
 +
  gnuplot> set grid    #turn grid on
 +
  gnuplot> unset grid  #turn grid off
  
 +
; autoscale
 +
: set xrange, yrange, or both "automatically" based on domain and range of data being plotted
 +
  gnuplot> set autoscale x  # autoscale the x axis
 +
  gnuplot> set autoscale y  # autoscale the y axis
 +
  gnuplot> set autoscale    # autoscale both axes
  
gnuplot> set style data linespoints
+
; logscale
gnuplot> rep
+
: set logscale for x, y, or both axes. note that gnuplot will complain if one of the axis ranges includes zero
 +
  gnuplot> set logscale x  # logscale the x axis
 +
  gnuplot> set logscale y  # logscale the y axis
 +
  gnuplot> set logscale    # logscale both axes
  
=====choosing and manipulating data=====
+
; key
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.
+
: set the placement of the key/legend
+
  gnuplot> set key left      # move key to left of plot
> cat simple2.txt
+
  gnuplot> set key right    # move key to right of plot
0.0  1.1  2.4  5.4
+
  gnuplot> set key top      # move key to to top of plot
0.1  1.3  2.7  6.3
+
  gnuplot> set key bottom    # move key to to bottom of plot
0.2  1.4  2.9  7.5
+
  gnuplot> unset key        # remove key
0.3  1.9  3.1  9.1
 
  
 +
; 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
  
;using
+
; samples
: select columns from
+
: the number of samples gnuplot uses when evaluating functions to plot
 +
  gnuplot> plot sin(5*x)    # peaks of sine wave are jagged because sample rate is too low 
 +
  gnuplot> set samples 1000  # evaluate function of 1000 points when creating plot
 +
  gnuplot> rep              # peaks are smoother because more points are used
 +
  gnuplot> set samples 5000; rep # wow! even smoother
  
====lines vs. points====
+
; terminal
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).
+
: 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)
 +
  gnuplot> set terminal x11 0      # send plot to X11 first x11 termainal (same as above)
 +
  gnuplot> set terminal x11 1      # send plot to X11 second x11 termainal (will plot in a new window)
  
==Creating Pictures==
+
; output
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.
+
: set name of a file to write output to. this is used together with the <code>terminal</code> option.
 +
  gnuplot> set output "filename.png"        # write in png format
 +
  gnuplot> set output "filename.postscript" # write in postscript format
 +
  gnuplot> set output "filename.jpeg"      # write in jpeg format
  
To change the terminal type, set the <code>terminal</code> option. For example, to create a png, set the terminal type to png.
+
==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 <code>terminal</code> option to the format you want to save the plot as, then set the <code>output</code> option to the name of the file.
  
 
  gnuplot> set terminal png
 
  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==
 
==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 <b>grace</b>.  The <b>grace</b> 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==
 +
 +
* [http://www-128.ibm.com/developerworks/library/l-gnuplot/ Visualize your data with gnuplot]: an IBM tutorial
 +
* [http://www.gnuplotting.org/ gnuplotting]: a blog of gnuplot examples and tips
 +
* [http://spplotters.blogspot.com/ spplotters]: 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://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

Latest revision as of 10:46, 26 February 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. 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 in a plot command.

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. Options are set with 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 after options are set. To update the plot, either reissue the same plot command, or just use the replot command.

gnuplot> replot  # redraw the previous plot with new options

See the #Plot Display Options 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), gauss2(x/sigma)

Plotting Data from a File

gnuplot is very useful for plotting arbitrary functions, but it really shines when it comes to plotting data in a file. gnuplot makes it simple to quickly plot data stored in a plain text file. Typically this has either been generated from a computer model, or collected in an experiment. Here are two small text files containing data that can be used to follow the examples below.

simple.txt

simple2.txt

large-data.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. The first column is used for the x coordinates and the second column is used for the y coordinates,

gnuplot> plot 'simple.txt'  # note the quotes. single or double quotes can be used

This turns out to be very convenient because this is about the simplest format to write data to from within a program

 // arrays x and y contain the x and y coordinates of our data
 // 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;
More Sophisticated Data Files

gnuplot can plot data from files even if there are more than two columns. By default, gnuplot will read the file and use the first two columns and ignore the rest.

> 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, but can also be used to manipulate the data before it is plotted. For example, to multiply all y values by 4.5 and plotting the results,

gnuplot> plot 'simple2.txt' using 1:(4.5*$3)

Notice the parenthesis and the $3. If an integer is given, the integer is taken to be the column number to use. If a calculation involving columns is to be used, it must be enclosed with parenthesis and the columns are refered to with a '$' (similar to gawk). Calculations involving columns can use data from any column. The using command can be used to do some very powerful things

gnuplot> plot 'simple2.txt' using 1:($2*$1)                          # plot column 1 times column 2 vs column 1
gnuplot> plot 'simple2.txt', 'simple2.txt' using 1:(1.5 * $1 + 1.1)  # plot column 2 vs column 1 and and the line 1.5x + 1.1 using x values from column 1
gnuplot> plot 'simple2.txt' using ($1 - 10):2                        # plot column 2 vs column 1, but shift the x axis by 10
gnuplot> plot 'simple2.txt' using (2*$1):(5*exp( $2 ) )              # plot column 2 vs column 1, manipulating both first

Sometimes, it may be useful to only plot some of the points in a data file. For example, if the data file is very large, it can take a while for gnuplot to generate the plot. By plotting only every other point, or every 10th point, the plot time can be shortened. To limit the number of points that are plotted from a data file, use the every modifier.

gnuplot> plot 'simple2.txt' every 2                          # plot every other line in simple2.txt using columns 1 and 2
gnuplot> plot 'simple2.txt' every 3 using 1:3                # plot every 3rd line in simple2.txt using columns 1 and 3

plot Command Modifiers

In its simplest form the plot command just takes a list of functions or files to plot. However, each plot can be modified by specifying a set of modifiers. The modifiers are just given after the function or file to be plotted. For example, using and every are modifiers that can be applied to data files.

using
select which columns in a data file to plot
gnuplot> plot 'data.txt' using 1:3
every
only plot some lines from a data file
gnuplot> plot 'data.txt' every 2
title
set the string used for a plot in the key/legend
gnuplot> plot 'data.txt' title "raw data",  using 1:(2*$2) title "processed data", x*x 'quadradic'

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
xtics, ytics
set values marked on the x or y axis.
 gnuplot> set xtics 1   # mark every whole number on the x axis
 gnuplot> set ytics 2   # mark every even number on the y axis
grid
draw a grid on the plot canvas.
 gnuplot> set grid    #turn grid on
 gnuplot> unset grid  #turn grid off
autoscale
set xrange, yrange, or both "automatically" based on domain and range of data being plotted
 gnuplot> set autoscale x   # autoscale the x axis
 gnuplot> set autoscale y   # autoscale the y axis
 gnuplot> set autoscale     # autoscale both axes
logscale
set logscale for x, y, or both axes. note that gnuplot will complain if one of the axis ranges includes zero
 gnuplot> set logscale x   # logscale the x axis
 gnuplot> set logscale y   # logscale the y axis
 gnuplot> set logscale     # logscale both axes
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
samples
the number of samples gnuplot uses when evaluating functions to plot
 gnuplot> plot sin(5*x)     # peaks of sine wave are jagged because sample rate is too low  
 gnuplot> set samples 1000  # evaluate function of 1000 points when creating plot
 gnuplot> rep               # peaks are smoother because more points are used
 gnuplot> set samples 5000; rep # wow! even smoother
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)
 gnuplot> set terminal x11 0      # send plot to X11 first x11 termainal (same as above)
 gnuplot> set terminal x11 1      # send plot to X11 second x11 termainal (will plot in a new window)
output
set name of a file to write output to. this is used together with the terminal option.
 gnuplot> set output "filename.png"        # write in png format
 gnuplot> set output "filename.postscript" # write in postscript format
 gnuplot> set output "filename.jpeg"       # write in jpeg format

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