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

« back to all changes in this revision

Viewing changes to examples/demo/updating_plot/updating_plot2.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_plot.py to show 6 different plots whose data
4
 
are being modified.
5
 
 
6
 
Click and drag any of the plots to reposition them.
7
 
Right-click and drag inside the plots to pan them.
8
 
Mousewheel up and down to zoom.  Zoom box is availble (see
9
 
description in simple_line.py).
10
 
"""
11
 
 
12
 
# Major library imports
13
 
import wx
14
 
from numpy import arange
15
 
from scipy.special import jn
16
 
 
17
 
# Enthought library imports
18
 
from enable.api import Window
19
 
from enable.example_support import DemoFrame, demo_main
20
 
from traits.api import HasTraits
21
 
 
22
 
# Chaco imports
23
 
from chaco.api import *
24
 
from chaco.tools.api import MoveTool, PanTool, ZoomTool
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"):
33
 
        self.x_values = x[:]
34
 
        self.y_values = y[:]
35
 
        self.numpoints = len(self.x_values)
36
 
 
37
 
        plot = create_line_plot((self.x_values,self.y_values), color=color, bgcolor=bgcolor,
38
 
                                add_grid=True, add_axis=True)
39
 
        plot.resizable = ""
40
 
        plot.bounds = [PLOT_SIZE, PLOT_SIZE]
41
 
 
42
 
        plot.tools.append(PanTool(plot, drag_button="right"))
43
 
        plot.tools.append(MoveTool(plot))
44
 
        plot.overlays.append(ZoomTool(plot, tool_mode="box", always_on=False))
45
 
 
46
 
        plot.unified_draw = True
47
 
        self.plot = plot
48
 
 
49
 
        self.current_index = self.numpoints/2
50
 
        self.increment = 2
51
 
 
52
 
    def timer_tick(self):
53
 
        if self.current_index <= self.numpoints/3:
54
 
            self.increment = 2
55
 
        elif self.current_index == self.numpoints:
56
 
            self.increment = -2
57
 
        self.current_index += self.increment
58
 
        if self.current_index > self.numpoints:
59
 
            self.current_index = self.numpoints
60
 
        self.plot.index.set_data(self.x_values[:self.current_index])
61
 
        self.plot.value.set_data(self.y_values[:self.current_index])
62
 
        self.plot.request_redraw()
63
 
 
64
 
 
65
 
class PlotFrame(DemoFrame):
66
 
 
67
 
    def _create_data(self):
68
 
        values = [jn(i, x) for i in range(10)]
69
 
 
70
 
    def _create_window(self):
71
 
        numpoints = 50
72
 
        low = -5
73
 
        high = 15.0
74
 
        x = arange(low, high, (high-low)/numpoints)
75
 
        container = OverlayPlotContainer(bgcolor="lightgray")
76
 
 
77
 
        self.animated_plots = []
78
 
        for i, color in enumerate(COLOR_PALETTE):
79
 
            animated_plot = AnimatedPlot(x, jn(i,x), color)
80
 
            container.add(animated_plot.plot)
81
 
            self.animated_plots.append(animated_plot)
82
 
 
83
 
        for i, a_plot in enumerate(self.animated_plots):
84
 
            a_plot.plot.position = [50 + (i%3)*(PLOT_SIZE+50), 50 + (i//3)*(PLOT_SIZE+50)]
85
 
 
86
 
        # Set the timer to generate events to us
87
 
        timerId = wx.NewId()
88
 
        self.timer = wx.Timer(self, timerId)
89
 
        self.Bind(wx.EVT_TIMER, self.onTimer, id=timerId)
90
 
        self.timer.Start(100.0, wx.TIMER_CONTINUOUS)
91
 
 
92
 
        self.container = container
93
 
        return Window(self, -1, component=container)
94
 
 
95
 
    def onTimer(self, event):
96
 
        for plot in self.animated_plots:
97
 
            plot.timer_tick()
98
 
        return
99
 
 
100
 
 
101
 
if __name__ == "__main__":
102
 
    demo_main(PlotFrame, size=(1000,600), title="Updating line plot")
103
 
 
104
 
# EOF