LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 12-17-2007, 08:22 PM   #1
sydney-troz
Member
 
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.

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])
 
Old 12-19-2007, 06:52 AM   #2
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 30
(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!
 
Old 12-19-2007, 01:02 PM   #3
sydney-troz
Member
 
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.
 
  


Reply

Tags
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 09:42 PM
matlab graphing mshinska Programming 4 12-07-2005 09:22 AM
graphing calculator Four Programming 3 11-28-2005 06:28 PM
Graphing TuxFreak Linux - Software 1 02-27-2005 05:29 PM
Graphing AMMullan Linux - Software 1 11-16-2003 02:11 AM


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

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