Tutorial
Tutorial
Contents
1 Introduction 2
1.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Nomenclature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Computer System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Generalities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 Disclaimer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3 Block Data 6
5 Batch plotting 8
5.1 Simple nonGUI printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.2 Multiple graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.2.1 An example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1. Introduction 2
6 Fitting curves 10
6.1 Linear Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
6.2 Non-linear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
7 Transformations 11
7.1 Graphical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
7.1.1 Simple Geometrical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
7.1.2 Mathematical operations between sets . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
7.1.3 Feature extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
7.2 Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7.2.1 Dening a region . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7.2.2 Using regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
8 Using Pipes 13
8.1 Instead of data les . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
8.2 Named pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
9 Multiple Graphs 14
9.1 Selecting graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
9.1.1 Arranging a tableau of graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
9.1.2 Arranging individual graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
9.2 Overlaying Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
10 Hot Links 16
10.1 File containing a Single Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
10.2 Multiple sets within a le . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
10.3 Updating by hot keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1 Introduction
These tutorials assumes that you are a new user to Grace but are somewhat familiar with a windowing
system. They are designed to show you some of the basic operation of Grace as well as a few of its less
intuitive features. Please feel free to go beyond the bounds of the actions described herein and explore the
possibilities of using Grace. After all, you will be the one who benets.
1. Introduction 3
1.1 Purpose
The purpose of these tutorials are to give brief examples to show you the basics of how to do something.
Essentials and some of the more esoteric features of Grace will be demonstrated to give the user an idea of
the capabilities of this program. It is not possible to show everything that Grace is capable of doing. That
knowledge only comes with use and experimentation. I recommend that you do the tutorial and then by
playing around with things, you will begin to understand them. Finally, when you get stuck, read the user
guide to help you.
1.2 Nomenclature
In referring to what item to select, the tutorial will use something of the form snaf:/foo/bar/bell which means
that on the snaf popup, select from the foo menu the submenu bar and from the bar menu, the entry bell.
The popup main is the large one with the graph that pops up when you run xmgrace. If there is a space in
the menu item, it will be replaced by an underscore. So , if the menu item was actually "Bell jar" instead
of bell in the last example, it would be snaf:/foo/bar/Bell_jar.
Things that are to be typed in will be presented in a typewriter font, eg, type y = 3*sin(x).
Some of examples require you to input a data le or graph. In such instances, there should be a le in the
tutorial directory named data.N or N.agr where N is the tutorial number. For example, when doing tutorial
7.1.3, you should look for a le 7.1.3.agr. It is assumed that each major tutorial section starts with a clean
graph.
1.4 Generalities
A couple of points should be made about the GUI before we begin to make life easier.
1. It is often possible to select more than one item from a list at a time (some lists may prevent it when
it makes no sense.). Clicking on a list entry without any keyboard modier pressed will deselect all
other entries and select only that one. Depressing shift while clicking an entry selects all entries from
a previously selected entry to the currently selected one. Finally, depressing control allows one to
individually toggle the selection of an entry.
2. There are often two buttons on a pop up: Apply and Accept. Changes are not registered until either
of these buttons is pressed. The dierence between them is that Accept also closes the window.
1.5 Disclaimer
Even though I do my best to keep this up to date with the latest release, I cannot guarantee it. Think of
this a perpetual work in progress. Therefore, if something is wrong, you can notify me and I'll x it but
2. Making a Simple Graph 4
2.5 Legends
Since we have several lines in our graph, it makes sense that we label them with a legend so that other people
can gure out what they mean. The rst thing to do is to give each set a label. This is done by entering a
legend string for each set in the Set appearance popup. Now, from the Main form in the Graph appearance
popup, click on "Display legend" to see the legend box. The location and appearance of the box is controlled
3. Block Data 6
by clicking on the "Leg. box" tab. The appearance and spacing of the legend entries is controlled by the
"Legends" tab. For simplicity, label the sets alphabetically and then play with the appearance, etc. to get
something you like.
Specifying the placement of the graph by entering the coordinates can be painful, especially the ne tuning.
To alleviate this problem, a graphical method is also available, although not readily apparent. After a legend
appears, it may be dragged to a new location. To do this, press Ctrl-L with your mouse on the main canvas.
You should see the arrow cursor turn into a hand. If this doesn't work, double click on the main canvas (to
get its attention) and then press Ctrl-L. Click on the legend and drag it. To cancel the legend drag mode
(as with all other modes), click on mouse button 3.
2.6 A challenge
I got bored so I took the data les and produced my own, albeit ugly, graph. See if you can copy mygraph.png
3 Block Data
A block of data is a table of number which are interpreted as columns of numbers. How sets are created
from the columns depends on the information you want to extract from the le.
We rst need to read in a block of data. We do this from Main:Data/Import/ASCII . Select the le "3.dat"
and Load as "Block data". If the read was successful, a window should pop up asking you to create a set
from the block data. At the top it will list how many columns of data were read.
First we choose the type of set we would like. For now we'll stick with xy.
Next we choose which column of data contains the x-ordinate. If there is no column, we can select "index"
which will use the index into the column as the x ordinate starting from one.
The values Y1 through Y4 are used for selecting error bars as may be needed by other set types.
The last thing to specify is the graph into which to load the set if we have more than 1 set.
Finally, hitting accept will create the set.
If you close this window, it can reopened by bringing up a set list (eg. Main:Edit/Data_sets ) and then
selecting Create_new/From_block_data from the menu brought up by right clicking on the set list.
Try creating a new set of type XYdY. This is an XY curve with error bars. Try X, Y, and Y1(the error)
from dierent columns.
people) anywhere within the data set list (which may be empty) and select Create new. A menu with 4
dierent ways of creating new sets will be presented. We'll go through them one by one.
4.1 By formula
The load and evaluate window will pop up when this is selected.
1. The rst step is to set up the parameter mesh which will determine the range and sampling of the
variable $t. Most often, $t will simply be the abscissa.
2. Next, choose the type of set you would like to produce.
3. Using the syntax of the command language, an expression for x is entered which uses $t as the inde-
pendent variable. This can be an extremely complicated function.
4. Likewise, an expression for y is entered and for any other expressions that may be needed. Fields after
y are labelled y1, y2, y3 and y4. For example, if the set type xydxdy is chosen, y1 will hold dx and y2
will hold dy and it will be necessary to enter expressions for them.
5. Pressing apply or accept will perform the calculations and create the new set. You may have to
autoscale to see the new set.
1. To plot one cycle of a sine wave: Load: Set X, Start load at: 0, Stop load at: 2*pi, Length: 100,
X=$t, Y=sin($t)
2. A unit circle by parameterization: Start at:0, Stop at: 2*pi, Length: 100, X=cos($t), Y=sin($t)
4.2 In spreadsheet
If your system has the Xbae widget set, this choice brings up a spreadsheet like editor to allow one to enter
the points of the set by hand. Initially, it just has the point ( 0, 0 ). Clicking on add will insert a copy
of the currently selected row immediately below the selected row. Clicking delete will delete the row which
contains the cursor. This method is best suited to examining or modifying existing sets or creating very
small sets. The sets gets updated after one hits enter or leaves the cell.
5 Batch plotting
Grace supports a large number of command line options which allow the user to control the appearance and
placement of graphs. This can be very useful if you want to use it to quickly print something without going
through the GUI, use it within a script to automatically generate graphs, or have a plot come up already
congured which can be much quicker than going through the GUI menus.
1. The rst step in plotting multiple graphs is usually telling Grace how many graphs we have and how
to arrange them. The interpreter command "arrange" will do this. For example, if we want 4 graphs
arranged in a simple 2x2 table, we specify -pexec "arrange ( 2, 2, .1, .1,.1,ON,ON,ON)" The exact
meaning of all the options is explained in the reference manual.
2. Specify any global options.
3. Specify for each graph, the data to plot and any options. Options should be specied in the following
order:
(a) "-graph g" where g is the graph number starting at 0. This says to apply all following options to
this graph.
(b) Set any autoscaling options. Autoscaling is performed when the le is read; ergo, the autoscaling
must be specied BEFORE the le is read. Remember, this setting is persistent.
(c) Set the set type. This is also a persistent setting.
(d) Specify the graph type and the input le.
(e) If reading in block data, create the sets with the "-bxy" option.
(f) Specify any world scaling. It is important to do this AFTER sets are read (unless autoscaling is
o) as the graph gets rescaled when data is read in.
(g) Specify anything else
5. Batch plotting 9
5.2.1 An example
Let's try an example. We will assume 5 plots, the rst 4 of which are to be stacked vertically, and the fth
inset into the fourth. We wish to plot the les a.dat, b.dat, c.dat and d.dat with the inset graph being a
magnied portion of d.dat. Assume a.dat contains multiple columns of data, b.dat is a block of data from
which we wish to make a curve from columns 2 and 4 with the error given by column 3, c.dat is to be
represented as a bar graph, and for the inset graph, we wish to graph to region (0,0) to (1,1). This can be
accomplished by
gracebat -pexec "arrange (4,1,.1,.1,.1,ON,ON,ON)" -nxy a.dat -graph 1 -block b.dat -settype xydy -bxy 2:4:3
-graph 2 -settype bar c.dat -graph 3 -settype xy d.dat -graph 4 d.dat -world 0 0 1 1 -viewport .15 .3 .8 .88
Note that the graph numbers start at 0 and that 0 is the default so it does not have to be specied for the
rst graph.
6 Fitting curves
This tutorial will explain some of Grace's curve tting abilities. Grace can perform two types of ttings.
The rst type is regression or linear tting where optimization is done on a linear equation or an equation
which can be expressed in a linear form. This includes tting polynomials and certain forms of equations.
The other type of tting is nonlinear and allows for arbitrary user supplied functions.
Let's take a curve and see how each type of tting works. To begin, create a curve of the function y =
sqrt(x) + exp(x)/3 -1 over the range 0 to 3 with 100 points.
6.2 Non-linear
We pop up the widget by selecting Main:Data/Transformations/Non-linear curve tting . You may want to
kill all the sets except the original function and the extrapolated function at this point.
5. The solution process is iterative and you must click on a button to run a certain number of iterations.
You should see the parameters change and a curve created with these parameters. In this example,
the exact solution is reached within 5 steps. More parameters and more dicult functions may require
more steps in which case you may choose to run 20 or 100 or more steps. The newly created optimized
curve should converge with an increasing number of steps.
6. As in linear curve tting, you can choose to load the t function at the points of the original curve,
over an arbitrary range or load the error at each point. For comparison, load the tted curve over
the range (3,4) by selecting Nonlinear:Options/Load/Function and lling in the bottom of the widget.
How does the true answer compare to your previous extrapolation?
7. Fitting arbitrary curves can be a tricky business. Initial conditions are very important. If you don't
get a good t, you may have to experiment a lot with the initial parameters values.
8. Finally, note that the tted curve does not get added until the accept button is pressed. This allows
you to "fool around" until you get a good t without creating a lot of garbage sets.
7 Transformations
7.1 Graphical
7.1.1 Simple Geometrical
You can rotate sets around an arbitrary axis perpendicular to the canvas (e.g. the Z-axis). Also it is possible
to scale sets and translate them.
1. Read in 7.1.2.agr
2. Note how the abscissa are dierent. We begin by using interpolate to produce a third set which
is the second set sampled at the x values of the rst. Call up the Interpolate popup from
Main:Data/Transformations/Interpolation/splines ...
3. Select S1 as the source set but don't specify a destination set. A set will automatically be made. Use
the Strict, linear method, Sampling:Abscissas of another set and use S0 as the Sampling set. Pressing
Apply should produce a curve which is S1 interpolated at the points of S0. Note that the new curve
only exists over the portion of the x axis common to both curves.
7. Transformations 12
4. We still have a problem since set 2 is sampled the same as set 0 but has a smaller domain. We can
perform the computation only over the common region so we now interpolate on set 0 at points from
set 2 to produce set 3. Now set 2 and set 3 have the exact same abscissas.
5. Call up the command interpreter from Main:Window/Commands ...
6. We need to create a set to hold the result, S4. We can either make a copy (using
Main:Edit/Set_operations...) of S2 which will be guaranteed to have the proper size to hold out
result or we can use type a command: s4 length s2.length
7. We have to break up the computation into an x part and a y part. In this instance, we simply wish to
keep the same x values. The nal result will be put into set 4, so we issue the command: s4.x = s2.x
8. Now we can perform the math between our interpolated copies of sets 0 and 1: s4.y = cos(s3.y) +
sin(s2.y).
9. So where is the new set? It's there but it's hidden. Since we already have the command window open,
we can unhide the set by typing: S4 on. For the GUI minded (no oense intended), bring up a set
list with the set operations menu (eg. Main:Edit/Data_sets or Main:Plot/Set_appearance ), select set
4 and unhide it by selecting show from the operation menu (mouse button 3).
N.B. If the abscissas of the original curves had been the same, we could have started at step 5. If the
sampling had been the same we could have skipped step 4.
7.2 Restrictions
Often we only wish to examine part of a data set or perform transformations only on a portion of one.
Restrictions allow us to dene a region of the graph on which to perform operations.
Line type
Dene the ends of the line by clicking with mouse button 1.
Polygon type
From the dene region popup, choose a polygon type and then the dene button. Use mouse button
1 to pick the vertices of the polygon and then mouse button 3 when you are done.
Range type
From the dene region popup, choose a range type and then pick 2 points which dene the range.
8 Using Pipes
Pipes are a way of capturing the output of a running process without the intermediary step of pacing the
output in a le. Instead, the executing program puts the data in one end of the pipe, and Grace reads it
from the other end of the pipe.
1. Start a named pipe (you will have to nd the command specic to your operating system. For example,
it could be mkfo or mknod ): mkfifo pvc. If you do a directory listing, you should see the le pvc.
2. Start up Grace in the background using the named pipe option: xmgrace- npipepv c&. Grace is now
monitoring the pipe for any data which might be sent to it. It will interpret things as though they
were entered using the command interpreter.
3. For a simple test, we will create a simple graph over the pipe. From your command line, type: echo
"read \"8.2.dat\"" > pvc. (The back slashes are needed to escape the quotation marks so that
Grace really received the command :read "8.2.dat".) This just told Grace to read the le data. Now
we would like to autoscale. We could simply click on the button but the point is to use a named pipe.
This time we type echoautoscale > pvcfollowed byecho redraw > pvc. Your graph should now
have autoscaled and redrawn. Exit Grace with echoexit> pvc. You should also clean up by removing
pvc.
4. The true power in named pipes lies in driving Grace using another program. The controlling program
can open a named pipe for writing, which is treated as an ordinary le. It can be opened with the
fopen() function or whatever other I/O function you prefer. Commands and data are then written to
the le where they are interpreted by Grace.
9 Multiple Graphs
9.1 Selecting graphs
When multiple graphs are present, a graph is selected by clicking inside the graph frame. In cases where
graph frames overlap, clicking will cycle among the overlapping graphs.
It might be annoying if one is trying to work in a region of overlapping graphs. If will not be possible to
double click on something because the each click will be interpreted as a single click and you will only end up
changing the graph focus. In such an instance, turning o the graph selection by clicking might be desirable.
Choose Main:Edit/Preferences and then set Misc:Graph_focus to "As set". This means one must explicitly
set the focus. Simply bring up a graph list (eg. Main:Edit/Overlay_graphs is but one), select the graph you
want to work on and then, using the menu under mouse button 3, choose "Focus to".
1. Choose 3 rows and 3 columns and Apply. You should now see 9 graphs. The Order button refers to
the way the graphs are numbered. The beginning of the line on the diagram of the button shows which
graph is numbered 0 and how the numbers increase, by row or column.
2. You realize you need horizontal packing, i.e. no horizontal gap between graphs. Click on the Pack
button beside the Hgap/width input and then Apply.
3. Suddenly, you realize you only need 6 graphs and not 9. Choose 2 rows and press Apply. There is a
slight problem as graphs 6, 7 an 8 are still visible. This is a feature since you don't want to accidentally
kill a graph. You can kill the extra graphs by clicking on the "Kill extra graphs" check box. Now, any
graphs other than the explicitly arranged ones will be automatically killed.
4. The margins are controlled by the Page Osets, and the intergraph spacing by the Hgap and Vgap
inputs.
5. Press close to remove the window.
Note that only graphs which are selected are taken into consideration. So, if you wish to
reorganize your existing graphs, make sure they are selected or new ones may be created.
overlay, clicking will cycle between the graphs. Hence, if graph 1 is selecting, clicking within the frame
will toggle to graph 0.
5. Making sure that graph 0 is active, bring up the Axis properties widget. Now set the y axis title to
Gnus.
6. Select graph 1 as active as set the title as Gnats. Notice how it overlaps the Gnus. We want to put
this on the right side. From the axis label and bar tab, select label Properties/Side=Opposite.
7. Label the x axis to label it. If graph 1 is the current graph, noticed how it is greyed out because only
1 x axis need be active. Select graph 0 and you should now be able to alter the axis label.
8. You are ready to read in data. Just make sure the graph that is active when you read in the data (or
create your set) is the one in which you intend it to go.
10 Hot Links
Hot links are a way of of updating a set without having to delete it rst and then reread it. The Hot Links
window is opened available under Main:Data/Hot links .