Linux Setup Notes

name and address
created July 20, 2004

Plotting Bar Graphs in Xmgrace

Xmgr and xmgrace are two Linux programs that are widely used for creating scientific and technical graphs. While xmgr is much easier to use, and draws point-to-point graphs well, its support for bar graphs is rudimentary. In contrast, xmgrace handles bar graphs better, but its menu system is perverse, which makes it quite difficult to use.

xmgrace graph
Proof that it is possible.

This article describes how to create a bar graph with error bars in xmgrace, and set each bar to a different fill pattern. In xmgrace, this can only be done by loading each data point separately through a procedure that is non-intuitive, to say the least. I wrote this page to avoid having to figure out the arcane procedure for doing this every time.

Importing data

In this simple example, we will have seven bars. Each bar needs three numbers: an x value, a y value, and an error bar value.

  1. Create data file in one long row, for example:
        1  10.1  0.27     2  1.36  0.49      3  0.42  0.08  \  
        4  0.43  0.24     5  0.66  0.22      6  0.11  0.41   \
        7  6.6   2.01  

    That is, the first bar is 10.1 plus/minus 0.27, and the bars are to be evenly spaced at 1 unit intervals.
    Although it's broken up here into three lines to fit on my web page, the data should be a single row of numbers in groups of three: x, y, and dy. If you want to bunch some of the bars together, leave larger intervals between some of the x values.
  2. Don't open the data file yet. The graph type must be set to "XY chart" before reading the file, otherwise you will be prevented from plotting it as a bar graph.
    1. Plot | Graph Appearance
    2. Set Presentation Type XY Chart
    3. Apply
    4. Close
  3. Import the first data point from the data file.
    1. Data | Import | ASCII
    2. Select filename.
    3. Load as "block data". (Alternatively, you could load the file as "Single set". This would also create a bar graph, but all the bars would be the same color and pattern. In this case, you should put the data in a conventional format, with the x's, y's, and dy's in three separate columns. However, a bar graph in which all the bars look exactly alike is usually not very useful).
    4. The Edit Block Data dialog will open.
    5. Set type = BARDY
    6. Select columns 1, 2, and 3 for x, y, and y1 for graph number G0.
  4. If the graph isn't drawn automatically, click on ^$ to draw the graph. If you still don't see a graph, try setting the xmin and ymin to negative numbers. Sometimes xmgrace puts bars in the wrong place, such as at x=-1.

Load the remaining data points

  1. To load the next bar, you can't just import the next three columns. You have to define another graph that will be superimposed on the first graph.
    1. Edit | Arrange Graphs
    2. Matrix ... Rows = Change to 7
    3. Check "Add graphs as needed to fill the matrix"
    4. Apply
  2. Set the type of each graph to "XY Chart"
    1. Plot | Graph Appearance
    2. Click on the next graph (G1) in the listbox.
    3. Set presentation type = XY Chart
    4. Apply
    5. Click on the next graph (G2) in the listbox. Repeat with all the remaining graphs.
    6. Close
  3. Import the remaining data as before
    1. Data | Import | ASCII
    2. Load as Block Data
    3. Select columns 4,5,6 for graph G1
    4. Repeat for remaining graphs, using the same dialog box, using the appropriate columns for the x, y, and dy for each data point.
  4. Now there should be 7 short graphs lined up vertically as shown in the image below. xmgrace-screendump
    Next we will rescale them so they all are exactly the same size and scale.
    1. Plot | Axis Properties
    2. Uncheck "Display tick labels", Display axis bar", and "Display tick marks" for all but the first graph (G0).
    3. Edit Y Axis
    4. Start 0 Stop 20. This sets the scale.
    5. Select "Apply to all axes, all graphs" or "Apply to current axis, all graphs".
    6. Repeat with X axis
    7. Plot | Graph Appearance
    8. Click on G0 in the Graph list
    9. Set Viewport Xmin = 0.15, Xmax = 1
    10. Set Viewport Ymin = 0.15, Ymax = 0.85
    11. Repeat for all the other graphs G1 to G6.

Put all the bars together

Now you should have 7 weird looking bars on a single graph. There should not be any overlapping tick labels or tick marks. The next step is to align the bars. Remember, there are still seven graphs superimposed on the screen. Before the properties of a bar can be changed, it is necessary to change the focus to its corresponding graph.

  1. Make the bars bigger, one at a time.
    1. Plot | Set appearance
    2. Select set: Click on G0
    3. Main Tab
    4. Symbol properties: Type = None, Size = 301. This will make the first bar wider.
    5. Note that clicking on the graph list in the Set Appearance dialog has no effect. Xmgr used to have a separate option called "Set focus". This is missing in xmgrace; however, it is still possible to set the focus to the desired graph. To change the focus, keep the Set Appearance dialog open, and open the Plot | Graph Appearance dialog. Double-click in the Graph Appearance list on the desired graph. This will cause the set in the Set Appearance dialog to change.
    6. Repeat with G1 to G6 to make all the bars wider.
  2. Change the colors and fill patterns for each bar.
    1. Keep the Graph Appearance dialog open so you can select a graph.
    2. Open Plot | Set Appearance
    3. Click Symbols tab
    4. Select pattern in Symbol Fill Pattern popup menu.

Miscellaneous actions

Problems

1. Sometimes a dialog box gets stuck, and you can't type anything in its text box or select any menu items. If this happens, use the mouse to drag the dialog box a few pixels to a new location. This is not an xmgrace problem, but is a bug in Motif and occurs with many Motif applications, including Netscape.

2. As with xmgr, only a few primary colors are available. To create a new color, it's necessary to edit the PostScript output. Look for "setgray" or "setrgbcolor".

3. The printed output in xmgrace defaults to thick line widths. This is useful if you intend to photograph your plot or for publication, but for analyzing data thin lines are often better. I had to create a script named "fixgrace" that automatically resets all the lines to zero width:

   cat $1 | sed s/linewidth.1../"linewidth 0"/g > $1.a
   cat $1.a | sed s/"size 792, 612"/"size 756, 584"/g > $1.fixed 

4. It is also difficult to control the positioning of the graph on the page. Luckily, the PostScript output is conveniently organized, which makes it easy to edit the PostScript to get the graph positioned correctly. Look for lines like

   337 1075 translate 

and

   0.24 0.24 scale

5. Although the nonlinear curve fitting gave the calculated parameters, in my tests I had trouble getting it to give an estimate of the error. Upon inspection of the source code, I found that the error is not printed if any of the data points is zero.


Back