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

« back to all changes in this revision

Viewing changes to examples/demo/updating_plot/updating_plot4.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_plot3.py.
4
 
 
5
 
Three of the plots are now oriented vertically, but the dataspace of all
6
 
6 plots is still linked.  Panning along the X axis of a vertical plot
7
 
will move the Y axis of one of the horizontally-oriented plots, and vice
8
 
versa.
9
 
"""
10
 
 
11
 
# Major library imports
12
 
import wx
13
 
from numpy import arange
14
 
from scipy.special import jn
15
 
 
16
 
# Enthought library imports
17
 
from enable.api import Window
18
 
from enable.example_support import DemoFrame, demo_main
19
 
from traits.api import HasTraits
20
 
 
21
 
# Chaco imports
22
 
from chaco.api import *
23
 
from chaco.tools.api import MoveTool, PanTool, ZoomTool
24
 
 
25
 
 
26
 
COLOR_PALETTE = ("mediumslateblue", "maroon", "darkgreen", "goldenrod",
27
 
                 "purple", "indianred")
28
 
 
29
 
PLOT_SIZE = 250
30
 
 
31
 
class AnimatedPlot(HasTraits):
32
 
    def __init__(self, x, y, color="blue", bgcolor="white", orientation="h"):
33
 
        self.y_values = y[:]
34
 
        if type(x) == ArrayDataSource:
35
 
            self.x_values = x.get_data()[:]
36
 
            plot = create_line_plot((x, self.y_values), color=color,
37
 
                                    bgcolor=bgcolor, add_grid=True, add_axis=True,
38
 
                                    orientation=orientation)
39
 
        else:
40
 
            self.x_values = x[:]
41
 
            plot = create_line_plot((self.x_values,self.y_values), color=color,
42
 
                                    bgcolor=bgcolor, add_grid=True, add_axis=True,
43
 
                                    orientation=orientation)
44
 
 
45
 
        plot.resizable = ""
46
 
        plot.bounds = [PLOT_SIZE, PLOT_SIZE]
47
 
        plot.unified_draw = True
48
 
 
49
 
        plot.tools.append(PanTool(plot, drag_button="right"))
50
 
        plot.tools.append(MoveTool(plot))
51
 
        plot.overlays.append(ZoomTool(plot, tool_mode="box", always_on=False))
52
 
 
53
 
        self.plot = plot
54
 
        self.numpoints = len(self.x_values)
55
 
        self.current_index = self.numpoints/2
56
 
        self.increment = 2
57
 
 
58
 
    def timer_tick(self):
59
 
        if self.current_index <= self.numpoints/3:
60
 
            self.increment = 2
61
 
        elif self.current_index == self.numpoints:
62
 
            self.increment = -2
63
 
        self.current_index += self.increment
64
 
        if self.current_index > self.numpoints:
65
 
            self.current_index = self.numpoints
66
 
        self.plot.index.set_data(self.x_values[:self.current_index])
67
 
        self.plot.value.set_data(self.y_values[:self.current_index])
68
 
        self.plot.request_redraw()
69
 
 
70
 
 
71
 
class PlotFrame(DemoFrame):
72
 
 
73
 
    def _create_data(self):
74
 
        values = [jn(i, x) for i in range(10)]
75
 
 
76
 
    def _create_window(self):
77
 
        numpoints = 50
78
 
        low = -5
79
 
        high = 15.0
80
 
        x = arange(low, high, (high-low)/numpoints)
81
 
        container = OverlayPlotContainer(bgcolor="lightgray")
82
 
 
83
 
        common_index = None
84
 
        index_range = None
85
 
        value_range = None
86
 
        self.animated_plots = []
87
 
        for i, color in enumerate(COLOR_PALETTE):
88
 
            if not common_index:
89
 
                animated_plot = AnimatedPlot(x, jn(i,x), color)
90
 
                plot = animated_plot.plot
91
 
                common_index = plot.index
92
 
                index_range = plot.index_mapper.range
93
 
                value_range = plot.value_mapper.range
94
 
            else:
95
 
                if i % 2 == 1:
96
 
                    orientation = "v"
97
 
                else:
98
 
                    orientation = "h"
99
 
                animated_plot = AnimatedPlot(common_index, jn(i,x), color,
100
 
                                             orientation=orientation)
101
 
                plot = animated_plot.plot
102
 
                plot.index_mapper.range = index_range
103
 
                plot.value_mapper.range = value_range
104
 
 
105
 
            container.add(plot)
106
 
            self.animated_plots.append(animated_plot)
107
 
 
108
 
 
109
 
        for i, a_plot in enumerate(self.animated_plots):
110
 
            a_plot.plot.position = [50 + (i%3)*(PLOT_SIZE+50), 50 + (i//3)*(PLOT_SIZE+50)]
111
 
 
112
 
 
113
 
        # Set the timer to generate events to us
114
 
        timerId = wx.NewId()
115
 
        self.timer = wx.Timer(self, timerId)
116
 
        self.Bind(wx.EVT_TIMER, self.onTimer, id=timerId)
117
 
        self.timer.Start(100.0, wx.TIMER_CONTINUOUS)
118
 
 
119
 
        self.container = container
120
 
        return Window(self, -1, component=container)
121
 
 
122
 
    def onTimer(self, event):
123
 
        for plot in self.animated_plots:
124
 
            plot.timer_tick()
125
 
        return
126
 
 
127
 
 
128
 
if __name__ == "__main__":
129
 
    demo_main(PlotFrame, size=(1000,800), title="Updating line plot")
130
 
 
131
 
# EOF