473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. from pylab import *
  2.  
  3. # Turn on interactive mode for graphing
  4. ion()
  5. #hold(False)
  6. xlabel("time (seconds)")
  7. ylabel("concentration (particles/window)")
  8.  
  9. #graphLines = []
  10. graphData = []
  11. updateInterval = 0
  12.  
  13. def initGraph(interval, *colours):
  14.     """Initializes this module
  15.  
  16.     interval: the time, in seconds, btwn calls to updateGraph()
  17.     colours: list of 3-tuples (r,g,b) for each line's colour
  18.     """
  19.     updateInterval = interval
  20.     for c in colours:
  21.         line = plot([0],[0])[0]
  22.         line.set_color(c)
  23.         graphData.append({'x':[0],'y':[0]})
  24.         #graphLines.append(line)
  25.  
  26. def updateGraph(*cons):
  27.     """Updates graph data and draws it
  28.  
  29.     cons: a list of concentrations, the same length of colours arg to initGraph
  30.     """
  31.     #for line, con in zip(graphLines, cons):
  32.     for line, con in zip(graphData, cons):
  33.         #xdata, ydata = line.get_xdata(), line.get_ydata()
  34.         #xdata.append(xdata[-1] + updateInterval)
  35.         #ydata.append(con)
  36.         #line.set_data(xdata, ydata)
  37.         #plot(xdata, ydata)
  38.         line['x'].append(line['x'][-1] + updateInterval)
  39.         line['y'].append(con)
  40.  
  41.     plot(*[line['x'], line['y'] for line in graphData])
  42.  
Nov 22 '07 #1
2 2173
bartonc
6,596 Expert 4TB
As much as I dislike globals, working on the module level like this, it is convenient:
Expand|Select|Wrap|Line Numbers
  1. updateInterval = 0
  2.  
  3. def initGraph(interval, *colours):
  4.     """Initializes this module
  5.  
  6.     interval: the time, in seconds, btwn calls to updateGraph()
  7.     colours: list of 3-tuples (r,g,b) for each line's colour
  8.     """
  9.     global updateInterval
  10.     updateInterval = interval
  11.     for c in colours:
  12.         line = plot([0],[0])[0]
  13.         line.set_color(c)
  14.         graphData.append({'x':[0],'y':[0]})
  15.         #graphLines.append(line)
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.
Nov 22 '07 #2
thanks, i appreciate your help
Nov 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Constandinos Mavromoustakis | last post by:
Dear all, first we apologize if you receive multiple copies of this announcement. please see below if you are interested. Thank you in advance....
0
by: Constandinos Mavromoustakis | last post by:
http://agent.csd.auth.gr/~cmavrom -------------------------------------------------- ============================================================================ = 37th Annual Simulation...
0
by: Gus | last post by:
---------------------------------------------------------------------------- ------------------------------------ Call for Papers: 38th Annual Simulation Symposium Part of the 2005 Spring...
0
by: Karatza Helen | last post by:
Our apologies if you have received multiple copies -------------------------------------------------- Call for Papers: 38th Annual Simulation Symposium Part of the 2005 Spring Simulation...
1
by: fRzzy madayaz | last post by:
Hello, I'm new to this kind of applications. Can you give me the concept of making a real-time interactive application. Let's say, it's like MSN Messenger or Yahoo! Messenger, where several...
1
by: robby.walker | last post by:
Hi, I've just released a full-featured online graphing calculator at http://www.e-tutor.com/et2/graphing/ It requires no downloads or plugins - just JavaScript. You can track points on the...
3
by: informat | last post by:
Hi : I have a very difficulty question ... I want to program a Simulation for a windows interactive exam .. if someone watched a Testout MCSE Class , he would see an Exam which a Windows...
0
by: Dexter | last post by:
GraphEasyRect was previously available as an applet. Now we have converted it to a desktop standalone application that runs on multiple platforms Windows, Mac, and Linux This tool allows...
1
by: skeates | last post by:
Hey all - I'm attempting to write a program that accesses a database, runs some calculations according to user input, and then creates some graphs from the results. The first two steps were pretty...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.