HeaderHeader

Introductory Graphing

Introduction

For most of the assignments in Physics 20, 21, and 22, you will write programs that produce lists of data -- position vs. time, or x vs. y -- which must be graphed. There are several graphing programs on the lab workstations. This document will discuss Grace, GNUPlot, and Mathematica.

Your data

We will assume that you have a C (or C++, Pascal, Fortran, Python,...) program producing the data, which goes into a file. You should make a two-column list in plain text (ASCII), each line of which is one data point. Here isa simple example, a list of x and x^2 for integer values of x.

0.0 0.0
1.0 1.0
2.0 4.0
3.0 9.0
4.0 16.0

We will use this file, squares.dat, as an example. The spacing within the lines is not significant. Here is a C program to create this file.

#include <stdio.h>
main() {
  double x;
  FILE *datafile;
  
  datafile = fopen("squares.dat","w");

  for(x = 0.0; x < = 4.0; x += 1.0)
    fprintf(datafile, "%4.1f %4.1f\n", x, x * x);

  fclose(datafile);
}

Grace (a.k.a xmgrace) - an XWindows Graphing Program

Grace is a general plotting package which operates through an "easy-to-use" graphical interface. It has many options that can be used to make graphs look the way you want them to. This is the package I use for all of the 2d plots in my research, and I highly recommend it.

To plot our file squares.dat, simply type

> xmgrace squares.dat &

at the prompt (here the "&" will allow Grace to spawn a new window, returning the unix prompt to you.) Grace uses a graphical interface, so we will describe the most important menu commands here.

Clicking on the button marked AS (AutoScale) makes the plot area large enough to fit all data. Clicking on the x-axis or y-axis of the plot brings up a window in which you can set the scale of each axis (plus tick marks, axis labels, etc) to whatever you want, as well as change the axes to logarithmic. Clicking on any data point brings up a window with data set properties. Here you can change the colors and symbols of plots, give titles to data sets, etc. Selecting the menu File, submenu Print Setup controls printing to a printer or to a file. Grace can print postscript, pdf, jpeg, and other formats.

If you don't like specifying files on the command line, you can also open the data file squares.dat by selecting the "Import/ASCII" option from the Data menu. A file selector window will pop up in which you select the file name with the mouse or just type it in.

If you have more than 2 columns in your file, you can use the -block option on the command line to select which columns to plot. For example

> xmgrace -block squares.dat -bxy 1:3 &
plots column 1 versus 3, and

> xmgrace -block squares.dat -bxy 1:2 -bxy 1:3 &
plots column 1 versus 2 and column 1 versus 3 on the same graph (assuming that there is a column 3 in squares.dat). You can also open multiple-column files through the "Import/ASCII" option from the Data menu; to do this, you must set File Type to" X Y1 Y2...". Notice that as you open multiple files, Grace plots the new data along with the old. Grace has many other capabilities, including curve fitting and data manipulation, which you are welcome to explore.

For more information use the Help menu or type

> xmgrace -help

GNUPlot

GNUPlot (surprisingly, no relation to GNU Emacs) is a general X Windows graphing program with a text-driven interface. To start it just type gnuplot at the prompt. Here is an example.

> gnuplot

G N U P L O T  Linux version 3.5  patchlevel 3.50.1.17, 27 Aug 93
last modified Fri Aug 27 05:21:33 GMT 1993
...

gnuplot> set title "Squares"
gnuplot> plot "squares.dat" with linespoints
gnuplot> set term postscript
Terminal type set to 'postscript'
gnuplot> set output "| lpr -h -Ppost1"
gnuplot> replot
gnuplot> quit

The window, and the output file, look like this:

Some options:

gnuplot> plot "squares.dat"

plots just points, no lines;

gnuplot> plot "squares.dat" with lines

plots just lines, no points;

gnuplot> clear

clears the screen.

Now, what about printing to the printer? We gave an example above; here's how to print and continue working:

gnuplot> plot "squares.dat"
gnuplot> set term postscript
gnuplot> set output "| lpr -h -Ppost1"
gnuplot> replot
gnuplot> set term X11
gnuplot> set output
gnuplot> replot

If you are producing a plot of, for instance, x and y vs. t, then your data might look like:

#  T        X        Y       X+Y
0.0000   1.0000   0.0460   1.0460
0.1000   0.9823   0.4566   1.4389
0.2000   0.9298   0.8477   1.7775
0.3000   0.8443   1.2025   2.0468
0.4000   0.7290   1.5057   2.2347
0.5000   0.5878   1.7444   2.3322
0.6000   0.4258   1.9085   2.3342
0.7000   0.2487   1.9907   2.2394
0.8000   0.0628   1.9877   2.0505
0.9000  -0.1253   1.8995   1.7742
1.0000  -0.3090   1.7300   1.4210

Here, the columns are t, x, y, and x+y. Note the comment at the start, delineated by the "#" sign. The graphing commands could be:

gnuplot> plot "xyz.dat" using 1:2 with lines

plots just x versus t. Note that the order of the statements using 1:2 and with lines is important. Instead,

gnuplot> plot "xyz.dat" using 1:4 with lines 

plots x+y versus t. But it gets boring to add with lines every time you want to plot with lines, doesn't it? Let's set this style to be the default:

gnuplot> set data style lines
gnuplot> plot "xyz.dat" using 2:3

plots x versus y, which yields nice lissajous figures.

gnuplot> plot "xyz.out" using 1:3, "xyz.out" using 1:2

This enables you to produce the two plots x vs. t and y vs. t overlayed on each other in different colors. Some other potentially useful commands:

gnuplot> plot sin(x)

This plots the sine function without needing any data file.

gnuplot> plot "squares.dat" title 'X^2 vs X'

Produces a plot of squares.dat with a title in the legend.

gnuplot> plot [-1:5]"squares.dat"

Forces the X range of the plot to be from -1 to 5.

gnuplot> help

Here you can access help on all aspects of GNUPlot.

Automation

GNUPlot really shines when you start automating it. If you are creating a plot with a bunch of complex commands, you can put them into a file that will automatically generate the plot for you. http://t16web.lanl.gov/Kawano/gnuplot/index-e.html is an excellent resource for putting the GNU into plotting.
The file below is a small script that uses GNUPlot commands to create a graph of a function. Linux automatically uses GNUPlot to interpret it.
#!/usr/bin/gnuplot -persist

# demo-plot.plt - Automate plotting with GNUPlot. 
# First make this file executable and then execute it like so: 
# $ chmod u+x demo-plot.plt && ./demo-plot.plt

# Define the function to be plotted.
  z(x,y)=sin(x)*cos(y);
# Set plotting options

# Increase sampling accuracy
  set isosample 80,80;
# Draw only the surface 
  set hidden3d;
# Draw contours
  set contour base;

  set title "z(x,y)=sin(x)cos(y)";
  set xlabel "x"; set ylabel "y"; set zlabel "z";

# Generate the graph
  splot z(x,y)

# Write the graph in PNG format to demo-plot.png
  set terminal png color;
  set output "demo-plot.png";
  replot;

A Plot!

Mathematica

Mathematica is a mathematical environment that allows for additional operations other than plotting.

First, we need to scan the data into the program. This is accomplished by the ReadList command. Let's store the data as squares. So, let's type

> squares = ReadList["squares.dat", Number, RecordLists -> True]

We use the Number option to tell mathematica what we are scanning, and the RecordLists -> True is necessary for the data to be scanned into a 2 x 5 matrix. We can then plot the data by the ListPlot function.

> ListPlot[square]

Return to the Physics Computation Lab page.