~ubuntu-branches/ubuntu/precise/python-chaco/precise

« back to all changes in this revision

Viewing changes to examples/updating_plot/updating_plot5.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2008-12-29 02:34:05 UTC
  • Revision ID: james.westby@ubuntu.com-20081229023405-x7i4kp9mdxzmdnvu
Tags: upstream-3.0.1
ImportĀ upstreamĀ versionĀ 3.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
"""
 
3
A modification of updating_plot4.py, except now all the plots are transparent,
 
4
and the three horizontally oriented plots have linked data views, while
 
5
the vertical plots are all independent.
 
6
"""
 
7
 
 
8
# Major library imports
 
9
import wx
 
10
from numpy import arange, fabs, pi, sin
 
11
from scipy.special import jn
 
12
 
 
13
# Enthought library imports
 
14
from enthought.enable.api import Window
 
15
from enthought.enable.example_support import DemoFrame, demo_main
 
16
from enthought.traits.api import false, HasTraits
 
17
 
 
18
# Chaco imports
 
19
from enthought.chaco.api import *
 
20
from enthought.chaco.tools.api import MoveTool, PanTool, SimpleZoom
 
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="none", orientation="h"):
 
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
                                    orientation=orientation)
 
36
        else:
 
37
            self.x_values = x[:]
 
38
            plot = create_line_plot((self.x_values,self.y_values), color=color,
 
39
                                    bgcolor=bgcolor, add_grid=True, add_axis=True,
 
40
                                    orientation=orientation)
 
41
 
 
42
        plot.resizable = ""
 
43
        plot.bounds = [PLOT_SIZE, PLOT_SIZE]
 
44
        plot.unified_draw = True
 
45
 
 
46
        plot.tools.append(PanTool(plot, drag_button="right"))
 
47
        plot.tools.append(MoveTool(plot))
 
48
        plot.overlays.append(SimpleZoom(plot, tool_mode="box", always_on=False))
 
49
 
 
50
        self.plot = plot
 
51
        self.numpoints = len(self.x_values)
 
52
        self.current_index = self.numpoints/2
 
53
        self.increment = 2
 
54
 
 
55
    def timer_tick(self):
 
56
        if self.current_index <= self.numpoints/3:
 
57
            self.increment = 2
 
58
        elif self.current_index == self.numpoints:
 
59
            self.increment = -2
 
60
        self.current_index += self.increment
 
61
        if self.current_index > self.numpoints:
 
62
            self.current_index = self.numpoints
 
63
        self.plot.index.set_data(self.x_values[:self.current_index])
 
64
        self.plot.value.set_data(self.y_values[:self.current_index])
 
65
        self.plot.request_redraw()
 
66
 
 
67
 
 
68
class PlotFrame(DemoFrame):
 
69
 
 
70
    def _create_data(self):
 
71
        values = [jn(i, x) for i in range(10)]
 
72
 
 
73
    def _create_window(self):
 
74
        numpoints = 50
 
75
        low = -5
 
76
        high = 15.0
 
77
        x = arange(low, high, (high-low)/numpoints)
 
78
        container = OverlayPlotContainer(bgcolor="lightgray")
 
79
 
 
80
        common_index = None
 
81
        index_range = None
 
82
        value_range = None
 
83
        self.animated_plots = []
 
84
        for i, color in enumerate(COLOR_PALETTE):
 
85
            if i == 0:
 
86
                animated_plot = AnimatedPlot(x, jn(i,x), color)
 
87
                plot = animated_plot.plot
 
88
                common_index = plot.index
 
89
                index_range = plot.index_mapper.range
 
90
                value_range = plot.value_mapper.range
 
91
            elif i % 2 == 0:
 
92
                animated_plot = AnimatedPlot(common_index, jn(i,x), color)
 
93
                plot = animated_plot.plot
 
94
                plot.index_mapper.range = index_range
 
95
                plot.value_mapper.range = value_range
 
96
            else:
 
97
                animated_plot = AnimatedPlot(x, jn(i,x), color, orientation="v")
 
98
                plot = animated_plot.plot
 
99
 
 
100
            container.add(plot)
 
101
            self.animated_plots.append(animated_plot)
 
102
 
 
103
 
 
104
        for i, a_plot in enumerate(self.animated_plots):
 
105
            a_plot.plot.position = [50 + (i%3)*(PLOT_SIZE+50), 50 + (i//3)*(PLOT_SIZE+50)]
 
106
 
 
107
 
 
108
        # Set the timer to generate events to us
 
109
        timerId = wx.NewId()
 
110
        self.timer = wx.Timer(self, timerId)
 
111
        self.Bind(wx.EVT_TIMER, self.onTimer, id=timerId)
 
112
        self.timer.Start(100.0, wx.TIMER_CONTINUOUS)
 
113
 
 
114
        self.container = container
 
115
        return Window(self, -1, component=container)
 
116
 
 
117
    def onTimer(self, event):
 
118
        for plot in self.animated_plots:
 
119
            plot.timer_tick()
 
120
        return
 
121
 
 
122
 
 
123
if __name__ == "__main__":
 
124
    demo_main(PlotFrame, size=(1000,800), title="Updating line plot")
 
125
 
 
126
# EOF