Reinforcement Learning and Artificial Intelligence (RLAI) Graph.py: A Quick Graphing Tool for Python

The ambition of this web page is to provide information on the usage of the graphing package.

# Graph.py: A Quick-Graphing Tool for Python

by Richard S. Sutton
and Steph Schaeffer

This is a tool to enable users of Python to quickly graph lists of numbers. For example, if `data` is a list of numbers, then `graph(data)` will create a window with a graph of the numbers, appropriately scaled.  Alternatively, the data can be a list of (x, y) or [x, y] coordinates, e.g., [[x1, y1], [x2, y2] ...]. If you have several lists to be plotted on the same graph, you just make a list of them and graph that.

Graph is based on the graphics package G.  G and graph both rely on tk; you must have tk on your machine, and the Pyhton module Tkinter. You must also be running in a windowing environment. Note that you must execute the g command `gStartEventLoop() (`formerly` gMainloop())` (to start window event processing) before you will actually see the window. The window will appear behind your currently running application; you may need to move your window and click on the graph window to see it properly.

Now with histograms as well.

## Simple Examples

`graph([1, 2, 3, 3, 2, 1]) #Graphs the numbers, as heightsgraph([sin(x/10.0) for x in range(200)]) #Graphs (sin x); see figure belowgraph([[x1, y1], [x2, y2], [x3, y3]]) #Graphs line by x,y coordinatesgraph([x/100.0, sin(x/100.0)] for x in range(314)]) #Detailed graph of (sin x)graph([[sin(x) for x in range(0,20)], \ #Graphs (sin x) and cos(x) [cos(x) for x in range(0,20)]]) #as 2 lines on the same graph`
The second example above, for example, produces:

## Multiple Graphs

All of the above create a new graph window to hold the graph, or take over the first existing graph window if there is one. If you wish to use multiple graphs, you need to give them names in the forms of strings that are the titles of the graph's windows. The name-strings are an additional argument to graph, as in these examples:

`graph([sin(x) for x in range(20)], None, "Sine") ; new graph windowgraph(cos(x) for x in range(20)], None, "Cosine") ; 2nd graph windowgraph(sin(x) for x in frange(0, 3.14, .01)], None, "Sine") ; replace first sin graph`

## Controlling Line Colors

Each line plotted on the graph will normally be a different color, chosen by some default way to be different from the other colors. If you want a line to be a particular color, you indicate this by a second argument to graph, as in:

`graph([sin(x) for x in range(20)], "red") ; red sin linegraphMore([cos(x) for x in range(20), "blue") ; second blue line on same graph`
Allowed values for the color argument are: "blue", "red", "green", "black", "yellow", "pink", "cyan", "purple", "magenta", "orange", "brown", "lightBlue", "gray", "darkGreen", "tan", "white", "lightGray", "darkGray". In addition to keywords, one can also use Macintosh color codes here, such as those produced by the color routines of the G graphics package. When specifying a color, a graph name can still be used, as a third argument, as in
`graphMore([sin(x) for x in range(20)], "red", "My Graph") ; red sin line`

## Adding to an Existing Graph

You can add a line to an existing graph by using graphMore instead of graph, as in the last two examples.

## Histograms

You can make simple histograms of data by calling histogram(list-of-data). This produces a graph depicting the distribution of numbers in the data list. For example, histogram([randrange(10) for i in range(100)]) produces:

Additional histograms can be added to a graph by calling histogramMore. Both routines take arguments:

`data a list of data to make the histogram withnumbins number of bins/intervals to use in the histogramminex minimum value of data included in the histogrammaxex maximum value - only data strictly less than this will be includedcolor a color to use for the histogram line, as in graphgraph a string indicating which graph to use, as in graph`
Thus, a more complete call to generate the above graph would be
histogram([randrange(10) for i in range(100)], 10, 0, 10, "red", "Histogram").
Minex and maxex default to include all the data. Numbins defaults to 30 or, in the case of integer data, to the integer span of the data, if the span is less than 200.

## Controlling Graph Limits and Scaling

The range of the graph is initially set to just cover the data. Alternatively, you can manually set the upper and lower limits of the graph's data space by calling
[function]
xGraphLimits(xmin, xmax, graphTitle)
[function]
yGraphLimits(ymin, ymax, graphTitle)

Sets the corresponding axis's limits and thus scales the data. If the min or max argument is None, then it is unchanged. If both are None, or absent, then the graph returns to automatic scaling. The last argument, graphTitle, specifies the graph involved, defaulting to the first graph as always.

Of course you can also change the scaling by grabbing the grow box of your graph and changing the size of the window.

## Controlling Tick Marks

By default graphs have tick marks at just the min and max of their x and y axes. Alternatively, you can manually set them by calling
[function]
xTickmarks(tickmarks, graphTitle)
[function]
yTickmarks(tickmarks, graphTitle)

Tickmarks can either be a list of actual tickmarks desired, or a number indicating the distance desired between tickmarks. GraphTitle specifies the graph involved, defaulting to the first graph as always.

For example, yTickmarks([0, .5, 1]) puts y-axis tick marks at 0, .5 and 1. yTickmarks(0.5) put y-axis tick marks on every half. You can get even more control by specifying the text labels for the tick marks, e.g., by yTickmarks([[0, "0.0"], [.5, "0.5"], [1, "1.0"]]) or
xTickmarks([[0, 'Up'], [1, 'Right'], [2, 'Down'], [3, Left']]). If no tick marks are specified, then the axis returns to automated tick-marking (just the min and max).

## Adding and Removing Lines of Existing Graphs

[function]
graphMore(data, color, graphTitle)

Plots additional lines, showing data, in the color indicated (if specified), on the first graph, or the graph indicated by graphTitle. The graph is rescaled if necessary to accomodate the new data.

[function]
graphLess(color, graphTitle)

Removes a line of data from the first graph, or the graph indicated by graphTitle. The line removed is given by color, e.g., "red", etc., as given above.

[function]
subtractFromGraph(linenumber, graphTitle)

Removes a line of data from the first graph, or the graph indicated by graphTitle. The line removed is given by linenumber, counting from 0, in the order the lines were added to the graph. If you remove line 0, all the others are of course renumbered so that they start at 0 again.

## Printing, Viewing, Miscellaneous

Use command key sequences for your particular platform to grab the appropriate portion of the screen and print it or put it in a file for later printing. For example, on the Mac, command-shift-4 saves a portion of the screen (which you highlight) into a pdf file.

Here are some tricks to ease examination of the contents of a graph. First select the graph and then hit the space-bar. The first line of the graph is highlighted. The left-right arrow keys move the highlighting from one line to the other in sequence, so each can be easily picked out from the others. Hitting the space-bar again turns off highlighting.

Move the cursor over the data space of a selected graph and it turns into a cross-hair. If you now click, the precise coordinates of the clicked point are printed to the shell window.

The function `gridGraph` lays a grid of dotted lines out from each tick mark, horizontally and vertically. A first optional argument specifies the density of the dots (by default it is 5, meaning every fifth pixel is dark). A second optional argument specifies the graph in the usual way with a graphTitle.

The function `graphPointsOnly`(graphTitle) changes the graph output from lines to dots at the data points. The optional graphTitle argument specifies the graph in the usual way. If you call it again, the output will again be lines (the routines toggles between dots and lines for output).

You can access the default graph window in the same way as the graphing routines do internally by calling `chooseGraph(graphTitle)`.

The default color of the nth line on a graph is determined by the internal function `nthColor(n)`. The current implementation returns the colors blue, red, green, black, yellow, pink, cyan, purple, magenta, orange, brown, lightBlue, gray, darkGreen, tan, white, lightGray, and darkGray, in that order starting from 0. You can change this simply by redefining `nthColor`. See the color specification routines in G for how to specify and construct colors.

## Obtaining and Installing Graph.py

Graph.py is built on the g graphics language which is in turn built on Tkinter (the Python version of tk). To install graph, you need to obtain the quickgraph file from the RLtoolkit page. This contains g, graph, and graph3d. (You will also need the Tkinter module that came with Python, and you must have Tk installed on your machine and be running in a windowing environment).

Once you have the files, here is the normal usage procedure:

If you installed Quickgraph, you can run Python from the directory where you put it, and import graph with:
`from Quickgraph.graph import *`
Note that you must execute the g command `gStartEventLoop()` before your graph window will show up.