2
The main executable file for the zoom_plot demo.
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.
10
# Standard library imports
13
# Major library imports
14
from numpy import sin, pi, linspace
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
23
from chaco.api import VPlotContainer, ArrayPlotData, Plot, PlotGrid, PlotAxis
24
from chaco.tools.api import RangeSelection
27
from zoom_overlay import ZoomOverlay
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,
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]
41
class ZoomPlot(HasTraits):
42
'''Encapsulation of the zoom plot concept.
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.
50
data = Instance(ArrayPlotData)
52
plot = Instance(Component)
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)
64
def _data_default(self):
65
x = linspace(0, 4*pi, 1201)
68
data = ArrayPlotData(x=x, y=y)
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='')
77
plotter2 = Plot(data=self.data)
78
zoom_plot = plotter2.plot(['x','y'])[0]
79
self.configure_plot(zoom_plot)
81
outer_container = VPlotContainer(padding=20,
84
stack_order='top_to_bottom',
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)
93
main_plot.controller = RangeSelection(main_plot)
95
zoom_overlay = ZoomOverlay(source=main_plot, destination=zoom_plot)
96
outer_container.overlays.append(zoom_overlay)
98
return outer_container
101
def configure_plot(plot, xlabel='Time (s)'):
102
""" Set up colors, grids, etc. on plot objects.
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
110
vertical_grid = PlotGrid(component=plot,
111
mapper=plot.index_mapper,
112
orientation='vertical',
115
use_draw_order = True)
117
horizontal_grid = PlotGrid(component=plot,
118
mapper=plot.value_mapper,
119
orientation='horizontal',
122
use_draw_order = True)
124
vertical_axis = PlotAxis(orientation='left',
125
mapper=plot.value_mapper,
126
use_draw_order = True)
128
horizontal_axis = PlotAxis(orientation='bottom',
130
mapper=plot.index_mapper,
131
use_draw_order = True)
133
plot.underlays.append(vertical_grid)
134
plot.underlays.append(horizontal_grid)
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)
141
#===============================================================================
142
# Attributes to use for the plot view.
146
#===============================================================================
147
# # Demo class that is used by the demo.py application.
148
#===============================================================================
149
class ZoomPlotView(HasTraits):
151
zoom_plot = Instance(ZoomPlot, ())
155
Item('object.zoom_plot.plot', editor=ComponentEditor(size=size),
157
orientation = "vertical"),
158
resizable=True, title='Zoom Plot',
159
width=size[0], height=size[1]
162
demo = ZoomPlotView()
163
# Configure the zoom plot by giving it data
165
x,y = read_music_data()
166
demo.zoom_plot.update_data(x, y)
171
if __name__ == "__main__":
172
demo.configure_traits()