Authors: | Klaus Muller <Muller@users.sourceforge.net> |
---|---|
SimPy release: | 2.2 |
Web-site: | http://simpy.sourceforge.net/ |
Python-Version: | 2.3 and later (not 3.0) |
Revision: | $Revision: 361 $ |
Date: | $Date: 2010-01-11 00:58:57 +1300 (Mon, 11 Jan 2010) $ |
Contents
The SimPlot plotting library has been developed for SimPy users so that they can produce, view and print simple plots, without having to download and install any other software package.
This manual is aimed at the SimPy applications programmer. It describes the capabilities of SimPlot and its programming interface.
There are several more elaborate Open Source plotting packages downloadable from the Internet which can be used from Python and therefore from SimPy. SimPlot is the “quick and dirty”, out-of-the-box plotting package for SimPy. If you need more complex plots or publication-quality graphics, consider using e.g. Matplotlib ( <http://matplotlib.sourceforge.net/>_ ).
SimPlot is a basic plotting package based on Tk/Tkinter and designed for use with SimPy. It has been developed from an Open Source plotting package published in John E. Grayson’s excellent book ‘Python and Tkinter Programming’ (ISBN 1-884777-81-3) which in turn was derived from Konrad Hinsen’s graph widget for NumPy (Numerical Python).
SimPlot provides for the generation, viewing and Postscript output of a variety of plot types by SimPy programs. The data series of SimPy Monitor instances can be plotted automatically.
SimPlot requires Tk/Tkinter to be installed (for all major operating systems on which Python runs, the Python installation already includes the installation of Tk/Tkinter, so no additional download or installation is required). Test whether Tk/Tkinter is installed by running ‘import Tkinter’ on the Python interpreter command line. If you don’t get an error message, it is.
To write SimPlot-based programs, only a very rudimentary understanding of Tk/Tkinter is required. This manual does not attempt to teach Tk/Tkinter!
A simple plot program using SimPlot basically looks like:
# Prog1.py
from SimPy.SimPlot import *
plt=SimPlot()
plt.plotLine([[0,0],[1,1],[2,4],[3,9]])
plt.mainloop()
When running this program, the resulting output on Windows is (the outside frame will look different on other platforms):
The program shows the basic structure of any program using SimPlot:
- Line 2 imports the plotting module,
- Line 3 creates and instance of the plotting class,
- Line 4 plots a line in an x/y coordinate system,
- Line 5 starts the main loop of Tk.
The frame also shows a ‘File’ menu item (when clicked, it offers a submenu item ‘Postscript’ which allows saving the plot to a Postscript file.
Method plotline has many name parameters with default values. Here is an example showing some of them (they will all be discussed further down in this manual:
# Prog2.py
from SimPy.SimPlot import *
plt=SimPlot()
plt.plotLine([[0,0],[1,1],[2,4],[3,9]],title="This is prettier",
color="red",width=2,smooth=True)
plt.mainloop()
This produces the following plot (the outside frame is not shown):
The plot now has a title and the curve is red, wider and smooth.
In addition to line plots, there are three other plot-types available in SimPlot, namely stepped line plots, bar charts, and scatter diagrams.
Here are examples of each. First, the stepped line plot:
# Prog3.py
from SimPy.SimPlot import *
plt=SimPlot()
plt.plotStep([[0,0],[1,1],[2,4],[3,9]],
color="red",width=2)
plt.mainloop()
which produces:
A bar chart program:
# Prog4.py
from SimPy.SimPlot import *
plt=SimPlot()
plt.plotBars([[0,0],[1,1],[2,4],[3,9]],
color="blue",width=2)
plt.mainloop()
which results in:
And finally, a scatter diagram:
# Prog5.py
from SimPy.SimPlot import *
plt=SimPlot()
plt.plotScatter([[0,0],[1,1],[2,4],[3,9]],
color="green",size=2,marker='triangle')
plt.mainloop()
and its output:
With a bit more involved programming, it is also possible to have several plots in one diagram and to have several diagrams in one Frame (just execute SimPlot.py to get these plots):
Note: In future versions of SimPlot, this part of the API will also be simplified so that it will require significantly less coding.
Class Monitor is the prime data collection tool for SimPy simulations. SimPlot therefore caters for easy plotting from Monitor intances.
Here is an example of a simple simulation using a Monitor:
# Monitorplot.py
from __future__ import generators
from SimPy.Simulation import *
from SimPy.Monitor import *
from SimPy.SimPlot import *
class Source(Process):
def __init__(self,monitor):
Process.__init__(self)
self.moni=monitor
self.arrived=0
def arrivalGenerator(self):
while True:
yield hold,self,uniform(0,20)
self.arrived+=1
self.moni.observe(self.arrived)
initialize()
moni=Monitor(name="Arrivals",ylab="nr arrived")
s=Source(moni)
activate(s,s.arrivalGenerator())
simulate(until=100)
plt=SimPlot()
plt.plotStep(moni,color='blue')
plt.mainloop()
This produces:
A SimPlot application program has the following structure:
- instantiation of the SimPlot class
- call of one or more plotting methods
- call to the SimPlot instance’s mainloop method
SimPlot exposes plotting methods at two levels, a simple API with limited capabilities but ease of use, and an advanced API with SimPlot’s full capabilities, but with more involved, verbose programming.
This section deals with the simple API.
Generates a line plot from a list of tuples (or lists) or from a Monitor (any instance which has the attributes ‘name’, ‘tlab’, ‘ylab’).
Parameters: |
|
---|---|
Return type: | Reference to GraphBase object which contains the plot. |
Generates a step plot from a list of tuples (or lists) or from a Monitor (any instance which has the attributes ‘name’, ‘tlab’, ‘ylab’). A horizontal line is drawn at a y-value until y changes, creating a step effect.
<variable> = <SimPlotInstance>.plotStep(values[,optional parameters])
param values: (mandatory) a list of two-element lists (or tuples), or a Monitor instance (any instance which has the attributes ‘name’, ‘tlab’, ‘ylab’)
Generates a bar chart plot from a list of tuples (or lists) or from a Monitor.
<SimPlotInstance>.plotBars(values[,optional parameters])
<variable> = <SimPlotInstance>.plotBars(values[,optional parameters])
Generates a histogram plot from a Histogram or a Histogram-like list or tuple. A SimPy Histogram instance is a list with items of two elements. It has n+2 bins of equal width, sorted by the first element, containing integer values == the counts of the bins. The first bin is the ‘under’ bin, the last the ‘over’ bin. Histogram objects are produced from Monitor objects by calling the Monitor method histogram().
<SimPlotInstance>.plotHistogram(values[,optional parameters])
<variable> = <SimPlotInstance>.plotHistogram(values[,optional parameters])
Generates a scatter diagram plot from a list of tuples (or lists) or from a Monitor.
<SimPlotInstance>.plotScatter(values[,optional parameters])
variable = <SimPlotInstance>. plotScatter(values[,optional parameters])
Saves Postscript output from a plot to a file. After e.g. aPlot=plotLine([0,1],[3,4]), aPlot.postscr("c:\\myplot.ps") outputs the line plot in Postscript to file c:\myplot.ps.
The advanced SimPlot API is more verbose than the simple one, but it offers more flexibility and power. The detailed structure of a program using that API is:
An example:
# AdvancedAPI.py
from SimPy. SimPlot import *
plt=SimPlot() # step 1
plt.root.title("Advanced API example") # step 3
line=plt.makeLine([[0,42],[1,1],[4,16]]) # step 4
bar=plt.makeBars([[0,42],[1,1],[4,16]],
color='blue') # step 4
sym=plt.makeSymbols([[1,1]],marker="triangle",
size=3,fillcolor="red") # step 4
obj=plt.makeGraphObjects([line,bar,sym]) # step 5
frame=Frame(plt.root) # step 6
graph=plt.makeGraphBase(frame,500,300,
title="Line and bars") # step 7
graph.pack() # step 8
graph.draw(obj) # step 9
frame.pack() # step 10
graph.postscr() # step 11
plt.mainloop() # step 12
Which generates:
Clearly, this level API is more verbose, but allows putting several diagrams with different plot types into one plot, or putting putting several plots into one frame (side by side, vertically, or in table fashion).
Assign a title to appear in the container’s title bar. (This is a method exposed by a Tk Toplevel container.)
Generates a line plot object from a list of tuples (or lists).
Generates a line plot object from a list of tuples (or lists). A horizontal line is generated at a y-value until y changes, creating a step effect.
Generates a scatter diagram plot object with markers from a list of tuples (or lists).
Generates a bar chart plot object with markers from a list of tuples (or lists).
Generates a histogram plot from a Histogram or a Histogram-like list or tuple. A SimPy Histogram instance is a list with items of two elements. It has n+2 bins of equal width, sorted by the first element, containing integer values == the counts of the bins. The first bin is the ‘under’ bin, the last the ‘over’ bin. Histogram objects are produced from Monitor objects by calling the Monitor method histogram().
Combines one or mor plot objects into one plottable GraphObject.
Generates a canvas widget in its Tk container widget (such as a Frame) with the plot’s background (title, axes, axis labels).
Controls how graph widget is arranged in its master container. (Inherited from Tk Packer geometry manager.)
Draws the plot background and the lines/curves in it.
Call:
<GraphBaseInstance>.draw(graph,[optional parameters])
After call to draw , saves Postscript output from a plot to a file.
Colours in SimPlot are defined by Tk. The Tk colour model is RGB. The simplest way to identify a colour is to use one of the hundreds of Tk-defined literals such as “black”, “aquamarine”, or even “BlanchedAlmond”. See the Tk colour page for definitions.
$Revision: 361 $ $Date: 2010-01-11 00:58:57 +1300 (Mon, 11 Jan 2010) $