~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to examples/demo/updating_plot/updating_plot3.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2014-06-01 17:04:08 UTC
  • mfrom: (7.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140601170408-m86xvdjd83a4qon0
Tags: 4.4.1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
 - Let the binary-predeb target work on the usr/lib/python* directory
   as we don't have usr/share/pyshared anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
"""
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.
6
 
"""
7
 
 
8
 
# Major library imports
9
 
import wx
10
 
from numpy import arange
11
 
from scipy.special import jn
12
 
 
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
17
 
 
18
 
# Chaco imports
19
 
from chaco.api import *
20
 
from chaco.tools.api import MoveTool, PanTool, ZoomTool
21
 
 
22
 
 
23
 
COLOR_PALETTE = ("mediumslateblue", "maroon", "darkgreen", "goldenrod",
24
 
                 "purple", "indianred")
25
 
 
26
 
PLOT_SIZE = 250
27
 
 
28
 
class AnimatedPlot(HasTraits):
29
 
    def __init__(self, x, y, color="blue", bgcolor="white"):
30
 
        self.y_values = y[:]
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)
35
 
        else:
36
 
            self.x_values = x[:]
37
 
            plot = create_line_plot((self.x_values,self.y_values), color=color,
38
 
                                    bgcolor=bgcolor, add_grid=True, add_axis=True)
39
 
 
40
 
        plot.resizable = ""
41
 
        plot.bounds = [PLOT_SIZE, PLOT_SIZE]
42
 
        plot.unified_draw = True
43
 
 
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))
47
 
 
48
 
        self.plot = plot
49
 
        self.numpoints = len(self.x_values)
50
 
        self.current_index = self.numpoints/2
51
 
        self.increment = 2
52
 
 
53
 
    def timer_tick(self):
54
 
        if self.current_index <= self.numpoints/3:
55
 
            self.increment = 2
56
 
        elif self.current_index == self.numpoints:
57
 
            self.increment = -2
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()
64
 
 
65
 
 
66
 
class PlotFrame(DemoFrame):
67
 
 
68
 
    def _create_data(self):
69
 
        values = [jn(i, x) for i in range(10)]
70
 
 
71
 
    def _create_window(self):
72
 
        numpoints = 50
73
 
        low = -5
74
 
        high = 15.0
75
 
        x = arange(low, high, (high-low)/numpoints)
76
 
        container = OverlayPlotContainer(bgcolor="lightgray")
77
 
 
78
 
        common_index = None
79
 
        index_range = None
80
 
        value_range = None
81
 
        self.animated_plots = []
82
 
        for i, color in enumerate(COLOR_PALETTE):
83
 
            if not common_index:
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
88
 
            else:
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)
94
 
 
95
 
 
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)]
98
 
 
99
 
 
100
 
        # Set the timer to generate events to us
101
 
        timerId = wx.NewId()
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)
105
 
 
106
 
        self.container = container
107
 
        return Window(self, -1, component=container)
108
 
 
109
 
    def onTimer(self, event):
110
 
        for plot in self.animated_plots:
111
 
            plot.timer_tick()
112
 
        return
113
 
 
114
 
 
115
 
if __name__ == "__main__":
116
 
    demo_main(PlotFrame, size=(1000,800), title="Updating line plot")
117
 
 
118
 
# EOF