Using GnuPlot for Bar Charts and Graphs

The sam_common_pylib/SamUtility package contains the GnuPlot module, which can be used to produce various types of bar charts.

Basic Usage:

    #!/usr/bin/env python
    import GnuPlot

    aPlot = GnuPlot.<plotType>( { 'outputFileBase' : 'nameOfOutput',
                                 [ 'debug' : 0 | 1 ],
                                 [ 'reuse-colors' : 0 | 1 ]
                               } )
    ...
    aPlot.plot( <theData> )
    aPlot.makeThumbnail()
    aPlot.makeJpg()
    aPlot.cleanup()
outputFileBase:
the basename of the output file(s).
debug:
debug flag; if set, intermediate files will not be deleted.
reuse-colors:
flag to re-use the [limited] number of colors in the color palatte; by default, colors will NOT be reused. (applies to YofXbyG and YofG1byG2 plots only; if there are too many groups, the highest contributors will be shown in unique colors, and the rest will be collected together as group 'other').
theData:
Structure containing the data to be plotted.

Available Plot Types:

Stacked Bar Charts:
PlotYofX Sample code and data PlotYbyG Sample code and data
PlotYofXbyG Sample code and data Positive and Negative PlotYofXbyG on Same Plot: Sample code and data
PlotYofG1byG2 Sample code and data Positive and Negative PlotYofG1byG2 on Same Plot: Sample code and data
Line Graphs (with Y errorbars)
GraphYofXperG Sample code and data GraphYofX-constantSigma Sample code and data
GraphYofG1perG2 Sample code and data GraphYofX-SigmaOfY Sample code and data
      GraphYofX-variableSigma Sample code and data

Useful Methods:

setTitle("titleString"):
Sets the title of the plot. If you want the title to be on multiple lines, you need to insert "\\n" (note, two back-slashes) where you want the line breaks.

setXlabel(label):
Sets the label on the X axis.

setYlabel(label):
Sets the label on the Y axis.

setKeyTitle(keytitle):
Sets the title above the legend.

setErrorbars(errorBarType, data):
For the Line Graphs, sets the value(s) of sigma (Y error bars). The type must be specified, and determines the format of the data:
  • errorBarType = 'constant': all error bars for the entire graph are identical and can be specified with one number. In this case, data is a number.
  • errorBarType = 'per_data_set' : each YofX has an error bar that is constant within this set of data; but they are not the same from Y1 to Y2. In this case, data is a dictionary of sigma values, where each group in the Y data is a key in the dictionary.
  • errorBarType = 'per_data_point' : each measurement has its own sigma; in this case, data is a list of dictionaries just like YofX.

setTimeFormat(inFormatArg, [outFormatArg]):
You must use this if your X-axis is a time or a date. Sets the format string for time arguments. inFormatArg describes the format of the data that is written to the GNUplot data file; that is, the format of the X-key to the YofX or YofXbyG dictionary. outFormatArg formats the Xaxis tic marcs; if no outFormatArg is specified, dates and times will be printed using inFormatArg.

setXrange(xmin, xmax)::
Sets the minimum and maximum x range on the plot.

setYrange(ymin, ymax):
Sets the mininum and maximum y range on the plot.

Example:

View the postscript, JPEG, and thumbnail plots produced by the code below (also available here).

#!/usr/bin/env python
import GnuPlot

# initialize the plot
aPlot = GnuPlot.PlotYofXbyG( {'outputFileBase': "IncomingFiles"} )

aPlot.setTitle("Incoming Files Per Data Tier")
aPlot.setTimeFormat("%d-%b-%Y", "%d-%b")
aPlot.setXlabel("Date")
aPlot.setYlabel("Number of Files")
aPlot.setKeyTitle("Data Tier:")

# Get the data
InFiles = {}

InFiles["01-Apr-2001"] = {}
InFiles["02-Apr-2001"] = {}
InFiles["03-Apr-2001"] = {}
InFiles["04-Apr-2001"] = {}
InFiles["05-Apr-2001"] = {}
InFiles["06-Apr-2001"] = {}
InFiles["07-Apr-2001"] = {}
InFiles["08-Apr-2001"] = {}
InFiles["09-Apr-2001"] = {}

InFiles["01-Apr-2001"]["digitized"] = 251.0
InFiles["01-Apr-2001"]["generated"] = 249.0
InFiles["01-Apr-2001"]["raw"] = 103.0
InFiles["01-Apr-2001"]["reconstructed"] = 256.0
InFiles["01-Apr-2001"]["simulated"] = 253.0
InFiles["02-Apr-2001"]["digitized"] = 200.0
InFiles["02-Apr-2001"]["generated"] = 201.0
InFiles["02-Apr-2001"]["raw"] = 122.0
InFiles["02-Apr-2001"]["reconstructed"] = 208.0
InFiles["02-Apr-2001"]["simulated"] = 201.0
InFiles["02-Apr-2001"]["special"] = 10.0
InFiles["03-Apr-2001"]["digitized"] = 189.0
InFiles["03-Apr-2001"]["generated"] = 200.0
InFiles["03-Apr-2001"]["raw"] = 88.0
InFiles["03-Apr-2001"]["reconstructed"] = 189.0
InFiles["03-Apr-2001"]["simulated"] = 189.0
InFiles["04-Apr-2001"]["digitized"] = 65.0
InFiles["04-Apr-2001"]["generated"] = 121.0
InFiles["04-Apr-2001"]["raw"] = 72.0
InFiles["04-Apr-2001"]["reconstructed"] = 63.0
InFiles["04-Apr-2001"]["simulated"] = 86.0
InFiles["05-Apr-2001"]["digitized"] = 101.0
InFiles["05-Apr-2001"]["generated"] = 45.0
InFiles["05-Apr-2001"]["raw"] = 58.0
InFiles["05-Apr-2001"]["reconstructed"] = 122.0
InFiles["05-Apr-2001"]["simulated"] = 77.0
InFiles["06-Apr-2001"]["digitized"] = 107.0
InFiles["06-Apr-2001"]["generated"] = 118.0
InFiles["06-Apr-2001"]["raw"] = 99.0
InFiles["06-Apr-2001"]["reconstructed"] = 113.0
InFiles["06-Apr-2001"]["root-tuple"] = 19.0
InFiles["06-Apr-2001"]["simulated"] = 114.0
InFiles["07-Apr-2001"]["digitized"] = 229.0
InFiles["07-Apr-2001"]["generated"] = 233.0
InFiles["07-Apr-2001"]["raw"] = 15.0
InFiles["07-Apr-2001"]["reconstructed"] = 248.0
InFiles["07-Apr-2001"]["root-tuple"] = 21.0
InFiles["07-Apr-2001"]["simulated"] = 229.0
InFiles["07-Apr-2001"]["special"] = 10.0
InFiles["08-Apr-2001"]["digitized"] = 143.0
InFiles["08-Apr-2001"]["generated"] = 149.0
InFiles["08-Apr-2001"]["raw"] = 98.0
InFiles["08-Apr-2001"]["reconstructed"] = 154.0
InFiles["08-Apr-2001"]["root-tuple"] = 12.0
InFiles["08-Apr-2001"]["simulated"] = 147.0
InFiles["09-Apr-2001"]["digitized"] = 132.0
InFiles["09-Apr-2001"]["generated"] = 112.0
InFiles["09-Apr-2001"]["raw"] = 77.0
InFiles["09-Apr-2001"]["reconstructed"] = 141.0
InFiles["09-Apr-2001"]["root-tuple"] = 33.0
InFiles["09-Apr-2001"]["simulated"] = 122.0

# plot it and clean up
aPlot.plot(InFiles)
aPlot.makeJpg()
aPlot.makeThumbnail()
aPlot.cleanup()

For further information, contact sam-design@fnal.gov.
This document last updated:
$Date: 2005/04/15 19:22:51 $
$Author: lauri $