Reinforcement Learning and
Artificial
Intelligence (RLAI) 

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.
graph([1, 2, 3, 3, 2, 1]) #Graphs the numbers, as heightsThe second example above, for example, produces:
graph([sin(x/10.0) for x in range(200)]) #Graphs (sin x); see figure below
graph([[x1, y1], [x2, y2], [x3, y3]]) #Graphs line by x,y coordinates
graph([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
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 namestrings are an additional argument to graph, as in these examples:
graph([sin(x) for x in range(20)], None, "Sine") ; new graph window
graph(cos(x) for x in range(20)], None, "Cosine") ; 2nd graph window
graph(sin(x) for x in frange(0, 3.14, .01)], None, "Sine") ; replace first sin graph
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 lineAllowed 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([cos(x) for x in range(20), "blue") ; second blue line on same graph
graphMore([sin(x) for x in range(20)], "red", "My Graph") ; red sin line
You can add a line to an existing graph by using graphMore instead of graph, as in the last two examples.
Additional histograms can be added to a graph by calling histogramMore. Both routines take arguments:
data a list of data to make the histogram withThus, a more complete call to generate the above graph would be
numbins number of bins/intervals to use in the histogram
minex minimum value of data included in the histogram
maxex maximum value  only data strictly less than this will be included
color a color to use for the histogram line, as in graph
graph a string indicating which graph to use, as in graph
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.
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 yaxis tick marks at 0, .5
and 1. yTickmarks(0.5) put yaxis 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 tickmarking (just
the min and max).
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.
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.
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.
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, commandshift4 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 spacebar. The first line of the graph is highlighted. The leftright 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 spacebar again turns off highlighting.
Move the cursor over the data space of a selected graph and it turns into a crosshair. 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.
Note that you must execute the g commandfrom Quickgraph.graph import *
gStartEventLoop()
before your graph
window will show up.