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

« back to all changes in this revision

Viewing changes to examples/zoomed_plot/zoom_plot.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-08 20:38:02 UTC
  • mfrom: (7.2.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110708203802-5t32e0ldv441yh90
Tags: 4.0.0-1
* New upstream release
* debian/control:
  - Depend on python-traitsui (Closes: #633604)
  - Bump Standards-Version to 3.9.2
* Update debian/watch file
* Remove debian/patches/* -- no longer needed

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.
7
 
"""
8
 
# Standard library imports
9
 
import os
10
 
 
11
 
# Major library imports
12
 
from numpy import amin, amax, arange, searchsorted, sin, pi, linspace
13
 
 
14
 
from enthought.enable.example_support import DemoFrame, demo_main
15
 
 
16
 
# Enthought imports
17
 
from enthought.enable.api import Component, ComponentEditor, Window
18
 
from enthought.traits.api import HasTraits, Instance
19
 
from enthought.traits.ui.api import Item, Group, View
20
 
from enthought.util.resource import find_resource
21
 
 
22
 
# Chaco imports
23
 
from enthought.chaco.api import SimplePlotFrame, VPlotContainer
24
 
from enthought.chaco.tools.api import RangeSelection
25
 
 
26
 
# Relative imports
27
 
from grid_plot_factory import create_gridded_line_plot
28
 
from zoom_overlay import ZoomOverlay
29
 
     
30
 
sample_path = os.path.join('examples','data','sample.wav')
31
 
alt_path = os.path.join('..','data','sample.wav')
32
 
fname = find_resource('Chaco', sample_path, alt_path=alt_path,
33
 
    return_path=True)
34
 
numpts = 3000
35
 
 
36
 
def read_music_data():
37
 
    from wav_to_numeric import wav_to_numeric
38
 
    index, data = wav_to_numeric(fname)
39
 
    return index[:numpts], data[:numpts]
40
 
 
41
 
def create_zoomed_plot():
42
 
    try:
43
 
        x,y = read_music_data()
44
 
    except:
45
 
        x = linspace(-10*pi, 10*pi, numpts)
46
 
        y = sin(x)
47
 
 
48
 
    main_plot = create_gridded_line_plot(x,y)
49
 
    zoom_plot = create_gridded_line_plot(x,y)
50
 
 
51
 
    outer_container = VPlotContainer(padding=30,
52
 
                                     fill_padding=True,
53
 
                                     spacing=50,
54
 
                                     stack_order='top_to_bottom',
55
 
                                     bgcolor='lightgray',
56
 
                                     use_backbuffer=True)
57
 
 
58
 
    outer_container.add(main_plot)
59
 
    outer_container.add(zoom_plot)
60
 
    
61
 
    main_plot.controller = RangeSelection(main_plot)
62
 
    
63
 
    zoom_overlay = ZoomOverlay(source=main_plot, destination=zoom_plot)
64
 
    outer_container.overlays.append(zoom_overlay)
65
 
 
66
 
    return outer_container
67
 
 
68
 
#===============================================================================
69
 
# Attributes to use for the plot view.
70
 
size = (800, 600)
71
 
title = fname
72
 
        
73
 
#===============================================================================
74
 
# # Demo class that is used by the demo.py application.
75
 
#===============================================================================
76
 
class Demo(HasTraits):
77
 
    plot = Instance(Component)
78
 
    
79
 
    traits_view = View(
80
 
                    Group(
81
 
                        Item('plot', editor=ComponentEditor(size=size), 
82
 
                             show_label=False),
83
 
                        orientation = "vertical"),
84
 
                    resizable=True, title=title,
85
 
                    width=size[0], height=size[1]
86
 
                    )
87
 
    
88
 
    def _plot_default(self):
89
 
         return create_zoomed_plot()
90
 
    
91
 
demo = Demo()
92
 
 
93
 
#===============================================================================
94
 
# Stand-alone frame to display the plot.
95
 
#===============================================================================
96
 
class PlotFrame(DemoFrame):
97
 
 
98
 
    def _create_window(self):
99
 
        # Return a window containing our plots
100
 
        return Window(self, -1, component=create_zoomed_plot())
101
 
    
102
 
if __name__ == "__main__":
103
 
    demo_main(PlotFrame, size=size, title=title)
104
 
 
105
 
#--EOF---