Generating graphs is not part of core Python so it was not included in the main chapter of this handbook. In this course you will use the Gnuplot package to generate graphical output. It is an extremely powerful and flexible program, although it does have a rather steep learning curve for beginners. However, you will use the Gnuplot.py package to allow you to control Gnuplot from within Python which makes things much easier.
In any program from which you would like graphical output you must include the line...
from Oxphys import *
...in the same way as you import the
libraries to have access to mathematical functions and arrays
Oxphys library has been written especially for
this course. You will find it gives you access to the
Gnuplot libraries, and a special function to
print your graphs.
And then, in the same way as you use the
open() function to get a
file ready, use the
Gnuplot() function to prepare a graph
g = Gnuplot()
g is merely a suggested name for your graph--you are free to
call it anything you would call any other variable.
Your program should then prepare two arrays to be plotted: one for the
-cooridinates, one for the . Let us assume two arrays exist.
xx contains the -coordinates of the data, and
yy the y
You then create a variable that points to the data using the
Data() function, which takes two array paramters. This creates a
compound variable type that holds all the information required to
generate a plot. Here we call our data variable
i results = Data(xx, yy)
You can then draw the plot to the screen using the command:
plot method takes one parameter: the
Data variable we
set up in the previous example.
This generates a simple, unlabelled plot. In general you will want to give your graphs titles and label the axes. Returning to the Geiger-Muller tube example, we might label our graph using the following commands:
g.title("Geiger-Muller counts made in one minute") g.xlabel("Time (minutes)") g.ylabel("Counts")
If you would like to join the points on your graph with straight lines
then, assuming your graph variable is called
g, use the following
g("set data style lines points")
This must be done before the graph is plotted using the
Depending on the structure of your program, you may find the graph
flashes up on the screen for such a short time that you cannot see it.
This is because of the way the
Gnuplot library works: the graph
will only remain as long as the Python module that created it is still
running. To insert a pause into your program then put a
raw_input() function after the
g.plot call. The
raw_input() function will wait for the user to press return. A
simple example is:
g.plot() raw_input("Press return to continue")
Note we have not done anything with the result of the
function. The user might, for example, have typed some text but we are
not interested in it and it is discarded.
If you want a print-out of the graph then use the
method. This takes usually takes no parameters (although still requires
the brackets). To use it simply include the line
(assuming your graph variable is called
So as to not waste paper we suggest you draw the graph to screen and then ask the user whether or not to print it out:
# Set up title/axes labels first g.plot() # Draw the graph on screen answer = raw_input("Would you like to print this graph? ") if answer == "y" or answer == "Y" or answer == "yes": g.hardcopy()
If you want to generate a Postscript file which can be included in a report (although note you are not expected to produce a report during this trial) then give the hardcopy method a filename as its parameter:
(It is conventional to add the
.ps extension to Postscript