I am writing a program to simulate a double displacement reaction getting to equilibrium, but I am having some trouble graphing the particles' concentrations. It seems that it's only possible to pan and zoom the graph once the program exits (when running it from IDLE), but I don't want to have to kill the sim every time I want to view the graph (besides, when I do, the graph window dissappears).
The following code contains all the graphing functionality, the initGraph() function is called before the sim's main loop, and updateGraph() is called once a second with the current concentrations of 4 types of particles. The sim uses PyGame for graphics, and I didn't think that code was relevant: if it is, just let me know and I'll post it.
Code:
from pylab import *
# Turn on interactive mode for graphing
ion()
#hold(False)
xlabel("time (seconds)")
ylabel("concentration (particles/window)")
#graphLines = []
graphData = []
updateInterval = 0
def initGraph(interval, *colours):
"""Initializes this module
interval: the time, in seconds, btwn calls to updateGraph()
colours: list of 3-tuples (r,g,b) for each line's colour
"""
updateInterval = interval
for c in colours:
line = plot([0],[0])[0]
line.set_color(c)
graphData.append({'x':[0],'y':[0]})
#graphLines.append(line)
def updateGraph(*cons):
"""Updates graph data and draws it
cons: a list of concentrations, the same length of colours arg to initGraph
"""
#for line, con in zip(graphLines, cons):
for line, con in zip(graphData, cons):
#xdata, ydata = line.get_xdata(), line.get_ydata()
#xdata.append(xdata[-1] + updateInterval)
#ydata.append(con)
#line.set_data(xdata, ydata)
#plot(xdata, ydata)
line['x'].append(line['x'][-1] + updateInterval)
line['y'].append(con)
plot(*[line['x'], line['y'] for line in graphData])