LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Realtime interactive graphing of a simulation (https://www.linuxquestions.org/questions/programming-9/realtime-interactive-graphing-of-a-simulation-607479/)

sydney-troz 12-17-2007 07:22 PM

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:

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])


enemorales 12-19-2007 05:52 AM

(Second try: I accidentally closed the tab with my response :()

Hi!

I think you need threads. They allow you to run different parts of your program in parallel. Take a look to the links (just googled "python threads"):

http://www.wellho.net/solutions/pyth...t-example.html

http://www.devshed.com/c/a/Python/Ba...ing-in-Python/

HTH!

sydney-troz 12-19-2007 12:02 PM

Yea, I have tried threads too (and will actually change the whole graphing mechanism to one that uses TCP for communication, so I can graph on a different machine etc.), but I have the same problem.


All times are GMT -5. The time now is 11:44 PM.