3
A modification of updating_plot2.py. Now instead of each plot having its
4
own dataspace, the data space "views" of all six plots are linked together,
5
so panning and zooming in one plot will also affect all the others.
8
# Major library imports
10
from numpy import arange
11
from scipy.special import jn
13
# Enthought library imports
14
from enable.api import Window
15
from enable.example_support import DemoFrame, demo_main
16
from traits.api import HasTraits
19
from chaco.api import *
20
from chaco.tools.api import MoveTool, PanTool, ZoomTool
23
COLOR_PALETTE = ("mediumslateblue", "maroon", "darkgreen", "goldenrod",
24
"purple", "indianred")
28
class AnimatedPlot(HasTraits):
29
def __init__(self, x, y, color="blue", bgcolor="white"):
31
if type(x) == ArrayDataSource:
32
self.x_values = x.get_data()[:]
33
plot = create_line_plot((x, self.y_values), color=color,
34
bgcolor=bgcolor, add_grid=True, add_axis=True)
37
plot = create_line_plot((self.x_values,self.y_values), color=color,
38
bgcolor=bgcolor, add_grid=True, add_axis=True)
41
plot.bounds = [PLOT_SIZE, PLOT_SIZE]
42
plot.unified_draw = True
44
plot.tools.append(PanTool(plot, drag_button="right"))
45
plot.tools.append(MoveTool(plot))
46
plot.overlays.append(ZoomTool(plot, tool_mode="box", always_on=False))
49
self.numpoints = len(self.x_values)
50
self.current_index = self.numpoints/2
54
if self.current_index <= self.numpoints/3:
56
elif self.current_index == self.numpoints:
58
self.current_index += self.increment
59
if self.current_index > self.numpoints:
60
self.current_index = self.numpoints
61
self.plot.index.set_data(self.x_values[:self.current_index])
62
self.plot.value.set_data(self.y_values[:self.current_index])
63
self.plot.request_redraw()
66
class PlotFrame(DemoFrame):
68
def _create_data(self):
69
values = [jn(i, x) for i in range(10)]
71
def _create_window(self):
75
x = arange(low, high, (high-low)/numpoints)
76
container = OverlayPlotContainer(bgcolor="lightgray")
81
self.animated_plots = []
82
for i, color in enumerate(COLOR_PALETTE):
84
animated_plot = AnimatedPlot(x, jn(i,x), color)
85
common_index = animated_plot.plot.index
86
index_range = animated_plot.plot.index_mapper.range
87
value_range = animated_plot.plot.value_mapper.range
89
animated_plot = AnimatedPlot(common_index, jn(i,x), color)
90
animated_plot.plot.index_mapper.range = index_range
91
animated_plot.plot.value_mapper.range = value_range
92
container.add(animated_plot.plot)
93
self.animated_plots.append(animated_plot)
96
for i, a_plot in enumerate(self.animated_plots):
97
a_plot.plot.position = [50 + (i%3)*(PLOT_SIZE+50), 50 + (i//3)*(PLOT_SIZE+50)]
100
# Set the timer to generate events to us
102
self.timer = wx.Timer(self, timerId)
103
self.Bind(wx.EVT_TIMER, self.onTimer, id=timerId)
104
self.timer.Start(100.0, wx.TIMER_CONTINUOUS)
106
self.container = container
107
return Window(self, -1, component=container)
109
def onTimer(self, event):
110
for plot in self.animated_plots:
115
if __name__ == "__main__":
116
demo_main(PlotFrame, size=(1000,800), title="Updating line plot")