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

« back to all changes in this revision

Viewing changes to examples/demo/zoomed_plot/zoom_plot.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
 
"""
2
 
The main executable file for the zoom_plot demo.
3
 
 
4
 
Right-click and drag on the upper plot to select a region to view in detail
5
 
in the lower plot.  The selected region can be moved around by dragging,
6
 
or resized by clicking on one of its edges and dragging. The ZoomPlot class
7
 
encapsulates the creation of a zoom plot and exposes some of the attributes and
8
 
methods necessary for deep interaction with the plot.
9
 
"""
10
 
# Standard library imports
11
 
import os
12
 
 
13
 
# Major library imports
14
 
from numpy import sin, pi, linspace
15
 
 
16
 
# Enthought imports
17
 
from enable.api import Component, ComponentEditor
18
 
from traits.api import HasTraits, Instance
19
 
from traitsui.api import Item, Group, View
20
 
from traits.util.resource import find_resource
21
 
 
22
 
# Chaco imports
23
 
from chaco.api import VPlotContainer, ArrayPlotData, Plot, PlotGrid, PlotAxis
24
 
from chaco.tools.api import RangeSelection
25
 
 
26
 
# Relative imports
27
 
from zoom_overlay import ZoomOverlay
28
 
 
29
 
sample_path = os.path.join('examples','data','sample.wav')
30
 
alt_path = os.path.join('..','data','sample.wav')
31
 
fname = find_resource('Chaco', sample_path, alt_path=alt_path,
32
 
    return_path=True)
33
 
numpts = 3000
34
 
 
35
 
def read_music_data():
36
 
    from wav_to_numeric import wav_to_numeric
37
 
    index, data = wav_to_numeric(fname)
38
 
    return index[:numpts], data[:numpts]
39
 
 
40
 
 
41
 
class ZoomPlot(HasTraits):
42
 
    '''Encapsulation of the zoom plot concept.
43
 
 
44
 
    This class organzies the data, plot container and ZoomOverlay required for
45
 
    a zoom plot. ZoomPlot represents the first step towards a reusable and
46
 
    extensible generalization of the zoom plot.
47
 
 
48
 
    '''
49
 
 
50
 
    data = Instance(ArrayPlotData)
51
 
 
52
 
    plot = Instance(Component)
53
 
    
54
 
    def update_data(self, x, y):
55
 
        '''Update the data in the plot windows'''
56
 
        # FIXME: This isn't forcing the update, so the crufty code below is used.
57
 
        #self.plot.data['x'] = x
58
 
        #self.plot.data['y'] = y
59
 
        self.plot.components[0].index.set_data(x)
60
 
        self.plot.components[0].value.set_data(y)
61
 
        self.plot.components[1].index.set_data(x)
62
 
        self.plot.components[1].value.set_data(y)
63
 
 
64
 
    def _data_default(self):
65
 
        x = linspace(0, 4*pi, 1201)
66
 
        y = sin(x**2)
67
 
 
68
 
        data = ArrayPlotData(x=x, y=y)
69
 
        
70
 
        return data
71
 
 
72
 
    def _plot_default(self):
73
 
        plotter = Plot(data=self.data)
74
 
        main_plot = plotter.plot(['x','y'])[0]
75
 
        self.configure_plot(main_plot, xlabel='')
76
 
 
77
 
        plotter2 = Plot(data=self.data)
78
 
        zoom_plot = plotter2.plot(['x','y'])[0]
79
 
        self.configure_plot(zoom_plot)
80
 
        
81
 
        outer_container = VPlotContainer(padding=20,
82
 
                                         fill_padding=True,
83
 
                                         spacing=0,
84
 
                                         stack_order='top_to_bottom',
85
 
                                         bgcolor='lightgray',
86
 
                                         use_backbuffer=True)
87
 
 
88
 
        outer_container.add(main_plot)
89
 
        outer_container.add(zoom_plot)
90
 
        # FIXME: This is set to the windows bg color.  Should get from the system.
91
 
        #outer_container.bgcolor = (236/255., 233/255., 216/255., 1.0)
92
 
 
93
 
        main_plot.controller = RangeSelection(main_plot)
94
 
        
95
 
        zoom_overlay = ZoomOverlay(source=main_plot, destination=zoom_plot)
96
 
        outer_container.overlays.append(zoom_overlay)
97
 
 
98
 
        return outer_container
99
 
 
100
 
    @staticmethod
101
 
    def configure_plot(plot, xlabel='Time (s)'):
102
 
        """ Set up colors, grids, etc. on plot objects.
103
 
        """
104
 
        plot.bgcolor = 'white'
105
 
        plot.border_visible = True
106
 
        plot.padding = [40, 15, 15, 20]
107
 
        plot.color = 'darkred'
108
 
        plot.line_width = 1.1
109
 
        
110
 
        vertical_grid = PlotGrid(component=plot,
111
 
                                mapper=plot.index_mapper,
112
 
                                orientation='vertical',
113
 
                                line_color="gray",
114
 
                                line_style='dot',
115
 
                                use_draw_order = True)
116
 
 
117
 
        horizontal_grid = PlotGrid(component=plot,
118
 
                                mapper=plot.value_mapper,
119
 
                                orientation='horizontal',
120
 
                                line_color="gray",
121
 
                                line_style='dot',
122
 
                                use_draw_order = True)
123
 
 
124
 
        vertical_axis = PlotAxis(orientation='left',
125
 
                                mapper=plot.value_mapper,
126
 
                                use_draw_order = True)
127
 
        
128
 
        horizontal_axis = PlotAxis(orientation='bottom',
129
 
                                title=xlabel,
130
 
                                mapper=plot.index_mapper,
131
 
                                use_draw_order = True)
132
 
 
133
 
        plot.underlays.append(vertical_grid)
134
 
        plot.underlays.append(horizontal_grid)
135
 
 
136
 
        # Have to add axes to overlays because we are backbuffering the main plot,
137
 
        # and only overlays get to render in addition to the backbuffer.
138
 
        plot.overlays.append(vertical_axis)
139
 
        plot.overlays.append(horizontal_axis)
140
 
        
141
 
#===============================================================================
142
 
# Attributes to use for the plot view.
143
 
size = (800, 600)
144
 
title = fname
145
 
 
146
 
#===============================================================================
147
 
# # Demo class that is used by the demo.py application.
148
 
#===============================================================================
149
 
class ZoomPlotView(HasTraits):
150
 
 
151
 
    zoom_plot = Instance(ZoomPlot, ())
152
 
    
153
 
    traits_view = View(
154
 
                    Group(
155
 
                        Item('object.zoom_plot.plot', editor=ComponentEditor(size=size), 
156
 
                             show_label=False),
157
 
                        orientation = "vertical"),
158
 
                    resizable=True, title='Zoom Plot',
159
 
                    width=size[0], height=size[1]
160
 
                    )
161
 
 
162
 
demo = ZoomPlotView()
163
 
# Configure the zoom plot by giving it data
164
 
try:
165
 
    x,y = read_music_data()
166
 
    demo.zoom_plot.update_data(x, y)
167
 
except:
168
 
    # Use the defaults
169
 
    pass
170
 
 
171
 
if __name__ == "__main__":
172
 
    demo.configure_traits()
173
 
 
174
 
#--EOF---