Menu

[r540]: / trunk / htdocs / examples / two_dymamic_images.py  Maximize  Restore  History

Download this file

60 lines (49 with data), 1.6 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
"""
An animated image
"""
import sys, time, os, gc
from matplotlib import rcParams
from matplotlib.matlab import *
import gtk
# if hold is on the axes images will accumulate and your performance
# will tank!
rc('axes', hold=False)
class HandleDraws:
drawing_idle_id = 0
shape = 100,100 # image size
cnt = 0
def __init__(self):
self.fig = figure(1)
self.a1 = subplot(211)
self.a2 = subplot(212)
def idle_update(self, *args):
'only call a draw if gtk is idle'
if self.cnt==0: self.tstart = time.time()
draw()
self.drawing_idle_id = 0
self.cnt += 1
if self.cnt>=50:
print 'FPS', self.cnt/(time.time() - self.tstart)
sys.exit()
return False
def update1(self, data):
if self.drawing_idle_id == 0:
self.a1.imshow(data, interpolation='nearest')
self.drawing_idle_id = gtk.idle_add(self.idle_update)
else: print 'dropping frame for axes 1'
def update2(self, data):
if self.drawing_idle_id == 0:
self.a2.imshow(data, interpolation='nearest')
self.drawing_idle_id = gtk.idle_add(self.idle_update)
else: print 'dropping frame for axes 2'
handler = HandleDraws()
def generate_events(*args):
data = rand(100,100)
# randomly pick which axes to update
if rand()>0.5: handler.update1(data)
else: handler.update2(data)
return True
cnt = 0
gtk.timeout_add(10, generate_events)
show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.