| 1 |
.. highlight:: python |
| 2 |
:linenothreshold: 5 |
| 3 |
|
| 4 |
======================================================== |
| 5 |
Publication-quality plot production with **matplotlib** |
| 6 |
======================================================== |
| 7 |
|
| 8 |
This document deals with producing production-quality plots from SimPy |
| 9 |
simulation output using the **matplotlib** library. matplotlib is known to |
| 10 |
work on Linux, Unix, MS Windows and OS X platforms. This library is not part |
| 11 |
of the SimPy distribution and has to be downloaded and installed separately. |
| 12 |
|
| 13 |
Simulation programs normally produce large quantities of output which needs to |
| 14 |
be visualized, e.g. by plotting. These plots can help with aggregating data, |
| 15 |
e.g. for detecting trends over time, frequency distributions or determining |
| 16 |
the warm-up period of a simulation model experiment. |
| 17 |
|
| 18 |
SimPy's SimPlot plotting package is an easy to use, out-of-the-box capability |
| 19 |
which can produce a full range of plot graphs on the screen and in PostScript |
| 20 |
format. After installing SimPy, it can be used without installing any other |
| 21 |
software. It is tightly integrated with SimPy, e.g. its Monitor data collection |
| 22 |
class. |
| 23 |
|
| 24 |
The SimPlot library is not intended to produce publication-quality plots. If |
| 25 |
you want to publish your plots in a report or on the web, consider using an |
| 26 |
external plotting library which can be called rom Python. |
| 27 |
|
| 28 |
|
| 29 |
About matplotlib |
| 30 |
==================== |
| 31 |
|
| 32 |
A very popular plotting library for Python is matplotlib. Its capabilities far |
| 33 |
exceed those of SimPy's SimPlot. This is how matplotlib is described on its |
| 34 |
home page: |
| 35 |
|
| 36 |
*"matplotlib is a python 2D plotting library which produces publication |
| 37 |
quality figures in a variety of hardcopy formats and interactive |
| 38 |
environments across platforms. matplotlib can be used in python scripts, |
| 39 |
the python and ipython shell (a la matlab or mathematica), web application |
| 40 |
servers, and six graphical user interface toolkits."* |
| 41 |
|
| 42 |
The matplotlib screenshots (with Python code) at |
| 43 |
http://matplotlib.sourceforge.net/users/screenshots.html show the great range |
| 44 |
of quality displays the library can produce with little coding. For the |
| 45 |
investment in time in downloading, installing and learning matplotlib, the |
| 46 |
SimPy user is rewarded with a powerful plotting capability. |
| 47 |
|
| 48 |
|
| 49 |
Downloading matplotlib |
| 50 |
-------------------------- |
| 51 |
|
| 52 |
You can download matplotlib from https://sourceforge.net/projects/matplotlib. |
| 53 |
Extensive installation instructions are provided at |
| 54 |
http://matplotlib.sourceforge.net/users/installing.html. |
| 55 |
|
| 56 |
|
| 57 |
matplotlib input data |
| 58 |
---------------------- |
| 59 |
|
| 60 |
matplotlib takes separate sequences (lists, tuples, arrays) for x- and |
| 61 |
y-values. SimPlot, on the other hand, plots Monitor instances, i.e., lists of |
| 62 |
[x,y] lists. |
| 63 |
|
| 64 |
This difference in data structures is easy to overcome in SimPy by using the |
| 65 |
Monitor functions ``yseries`` (returning the list of y-data) and ``tseries`` |
| 66 |
(returning the list of time- or x-data). |
| 67 |
|
| 68 |
|
| 69 |
An example from the Bank Tutorial |
| 70 |
------------------------------------- |
| 71 |
|
| 72 |
As an example of how to use matplotlib with SimPy, a modified version of |
| 73 |
bank12.py from the Bank Tutorial is used here. It produces a line plot of the |
| 74 |
counter's queue length and a histogram of the customer wait times:: |
| 75 |
|
| 76 |
#! /usr/local/bin/python |
| 77 |
""" Based on bank12.py in Bank Tutorial. |
| 78 |
This program uses matplotlib. It produces two plots: |
| 79 |
- Queue length over time |
| 80 |
- Histogram of queue length |
| 81 |
""" |
| 82 |
from SimPy.Simulation import * |
| 83 |
import pylab as pyl |
| 84 |
from random import Random |
| 85 |
## Model components |
| 86 |
class Source(Process): |
| 87 |
""" Source generates customers randomly""" |
| 88 |
def __init__(self,seed=333): |
| 89 |
Process.__init__(self) |
| 90 |
self.SEED = seed |
| 91 |
def generate(self,number,interval): |
| 92 |
rv = Random(self.SEED) |
| 93 |
for i in range(number): |
| 94 |
c = Customer(name = "Customer%02d"%(i,)) |
| 95 |
activate(c,c.visit(timeInBank=12.0)) |
| 96 |
t = rv.expovariate(1.0/interval) |
| 97 |
yield hold,self,t |
| 98 |
class Customer(Process): |
| 99 |
""" Customer arrives, is served and leaves """ |
| 100 |
def __init__(self,name): |
| 101 |
Process.__init__(self) |
| 102 |
self.name = name |
| 103 |
def visit(self,timeInBank=0): |
| 104 |
arrive=now() |
| 105 |
yield request,self,counter |
| 106 |
wait=now()-arrive |
| 107 |
wate.observe(y=wait) |
| 108 |
tib = counterRV.expovariate(1.0/timeInBank) |
| 109 |
yield hold,self,tib |
| 110 |
yield release,self,counter |
| 111 |
class Observer(Process): |
| 112 |
def __init__(self): |
| 113 |
Process.__init__(self) |
| 114 |
def observe(self): |
| 115 |
while True: |
| 116 |
yield hold,self,5 |
| 117 |
qu.observe(y=len(counter.waitQ)) |
| 118 |
## Model |
| 119 |
def model(counterseed=3939393): |
| 120 |
global counter,counterRV,waitMonitor |
| 121 |
counter = Resource(name="Clerk",capacity = 1) |
| 122 |
counterRV = Random(counterseed) |
| 123 |
waitMonitor = Monitor() |
| 124 |
initialize() |
| 125 |
sourceseed=1133 |
| 126 |
source = Source(seed = sourceseed) |
| 127 |
activate(source,source.generate(100,10.0)) |
| 128 |
ob=Observer() |
| 129 |
activate(ob,ob.observe()) |
| 130 |
simulate(until=2000.0) |
| 131 |
qu=Monitor(name="Queue length") |
| 132 |
wate=Monitor(name="Wait time") |
| 133 |
## Experiment data |
| 134 |
sourceSeed=333 |
| 135 |
## Experiment |
| 136 |
model() |
| 137 |
## Output |
| 138 |
pyl.figure(figsize=(5.5,4)) |
| 139 |
pyl.plot(qu.tseries(),qu.yseries()) |
| 140 |
pyl.title("Bank12: queue length over time", |
| 141 |
fontsize=12,fontweight="bold") |
| 142 |
pyl.xlabel("time",fontsize=9,fontweight="bold") |
| 143 |
pyl.ylabel("queue length before counter",fontsize=9,fontweight="bold") |
| 144 |
pyl.grid(True) |
| 145 |
pyl.savefig(r".\bank12.png") |
| 146 |
|
| 147 |
pyl.clf() |
| 148 |
n, bins, patches = pyl.hist(qu.yseries(), 10, normed=True) |
| 149 |
pyl.title("Bank12: Frequency of counter queue length", |
| 150 |
fontsize=12,fontweight="bold") |
| 151 |
pyl.xlabel("queuelength",fontsize=9,fontweight="bold") |
| 152 |
pyl.ylabel("frequency",fontsize=9,fontweight="bold") |
| 153 |
pyl.grid(True) |
| 154 |
pyl.xlim(0,30) |
| 155 |
pyl.savefig(r".\bank12histo.png") |
| 156 |
|
| 157 |
Here is the explanation of this program: |
| 158 |
|
| 159 |
**Line number and explanation** |
| 160 |
|
| 161 |
01 |
| 162 |
Imports the matplotlib **pylab** module (this import form is needed to avoid |
| 163 |
namespace clashes with SimPy). |
| 164 |
|
| 165 |
63 |
| 166 |
Sets the size of the figures following to a width of 5.5 and a height of 4 inches. |
| 167 |
|
| 168 |
64 |
| 169 |
Plots the series of queue-length values (qu.yseries()) over their observation |
| 170 |
times series (qu.tseries()). |
| 171 |
|
| 172 |
65 |
| 173 |
Sets the figure title, its font size, and its font weight. |
| 174 |
|
| 175 |
67 |
| 176 |
Sets the x-axis label, its font size, and its font weight. |
| 177 |
|
| 178 |
68 |
| 179 |
Sets the y-axis label, its font size, and its font weight. |
| 180 |
|
| 181 |
69 |
| 182 |
Gives the graph a grid. |
| 183 |
|
| 184 |
70 |
| 185 |
Saves the plot under the given name. |
| 186 |
|
| 187 |
72 Clears the current figure (e.g., resets the axes values from the previous plot). |
| 188 |
|
| 189 |
73 |
| 190 |
Makes a histogram of the queue-length series (qu.series()) with 10 bins. The *normed* |
| 191 |
parameter makes the frequency counts relative to 1. |
| 192 |
|
| 193 |
74 |
| 194 |
Sets the title etc. |
| 195 |
|
| 196 |
76 |
| 197 |
Sets the x-axis label etc. |
| 198 |
|
| 199 |
77 |
| 200 |
Sets the y-axis label etc. |
| 201 |
|
| 202 |
78 |
| 203 |
Gives the graph a grid. |
| 204 |
|
| 205 |
79 |
| 206 |
Limits the x-axis to the range[0..30]. |
| 207 |
|
| 208 |
80 |
| 209 |
Saves the plot under the given name. |
| 210 |
|
| 211 |
Running the program above results in two PNG files. The first (``bank12.png``) |
| 212 |
shows the queue length over time: |
| 213 |
|
| 214 |
.. image:: /_static/images/matplotlib/bank12.png |
| 215 |
|
| 216 |
The second output file (``bank12histo.png``) is a histogram of the customer |
| 217 |
queue length at the counter: |
| 218 |
|
| 219 |
.. image:: /_static/images/matplotlib/bank12histo.png |
| 220 |
|
| 221 |
|
| 222 |
Conclusion |
| 223 |
============== |
| 224 |
|
| 225 |
The small example above already shows the power, flexibility and quality of the |
| 226 |
graphics capabilities provided by matplotlib. Almost anything (fonts, graph |
| 227 |
sizes, line types, number of series in one plot, number of subplots in a plot, |
| 228 |
...) is under user control by setting parameters or calling functions. |
| 229 |
Admittedly, it initially takes a lot of reading in the extensive documentation |
| 230 |
and some experimentation, but the results are definitely worth the effort! |