Realtime interactive graphing of a simulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sydneytroz
    New Member
    • May 2007
    • 7

    Realtime interactive graphing of a simulation

    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=python]
    from pylab import *

    # Turn on interactive mode for graphing
    ion()
    #hold(False)
    xlabel("time (seconds)")
    ylabel("concent ration (particles/window)")

    #graphLines = []
    graphData = []
    updateInterval = 0

    def initGraph(inter val, *colours):
    """Initiali zes 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.appen d({'x':[0],'y':[0]})
    #graphLines.app end(line)

    def updateGraph(*co ns):
    """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(x data[-1] + updateInterval)
    #ydata.append(c on)
    #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])
    [/CODE]
    Last edited by bartonc; Nov 22 '07, 07:19 AM. Reason: Added =python to opening code tag
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    As much as I dislike globals, working on the module level like this, it is convenient:[CODE=python]
    updateInterval = 0

    def initGraph(inter val, *colours):
    """Initiali zes this module

    interval: the time, in seconds, btwn calls to updateGraph()
    colours: list of 3-tuples (r,g,b) for each line's colour
    """
    global updateInterval
    updateInterval = interval
    for c in colours:
    line = plot([0],[0])[0]
    line.set_color( c)
    graphData.appen d({'x':[0],'y':[0]})
    #graphLines.app end(line)[/CODE]Otherwise, updateInterval remains a function-scope variable and nobody else sees the change.

    That's the first thing that caught my eye... I'll keep looking.

    Comment

    • sydneytroz
      New Member
      • May 2007
      • 7

      #3
      thanks, i appreciate your help

      Comment

      Working...