Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-17-2007, 07:22 PM   #1
Registered: Feb 2007
Distribution: Kubuntu, it's obese barely-usable sibling, Ubuntu
Posts: 142

Rep: Reputation: 15
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.

from pylab import *

# Turn on interactive mode for graphing
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]

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)
        #line.set_data(xdata, ydata)
        #plot(xdata, ydata)
        line['x'].append(line['x'][-1] + updateInterval)
    plot(*[line['x'], line['y'] for line in graphData])
Old 12-19-2007, 05:52 AM   #2
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

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


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"):

Old 12-19-2007, 12:02 PM   #3
Registered: Feb 2007
Distribution: Kubuntu, it's obese barely-usable sibling, Ubuntu
Posts: 142

Original Poster
Rep: Reputation: 15
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.


python, simulation

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
interactive and non-interactive shell linuxjamil Programming 3 09-03-2006 08:42 PM
matlab graphing mshinska Programming 4 12-07-2005 08:22 AM
graphing calculator Four Programming 3 11-28-2005 05:28 PM
Graphing TuxFreak Linux - Software 1 02-27-2005 04:29 PM
Graphing AMMullan Linux - Software 1 11-16-2003 01:11 AM

All times are GMT -5. The time now is 03:23 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration