~ubuntu-branches/ubuntu/oneiric/python-chaco/oneiric

« back to all changes in this revision

Viewing changes to examples/demo/cursor_tool_demo.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
A Demonstration of the CursorTool functionality
 
3
 
 
4
Left-button drag to move the cursors round.
 
5
Right-drag to pan the plots. 'z'-key to Zoom
 
6
 
 
7
"""
 
8
# Major library imports
 
9
import numpy
 
10
 
 
11
# Enthought library imports
 
12
from chaco.api import create_line_plot, OverlayPlotContainer, \
 
13
             HPlotContainer, Plot, ArrayPlotData, jet
 
14
from chaco.tools.api import PanTool, ZoomTool
 
15
from chaco.tools.cursor_tool import CursorTool, BaseCursorTool
 
16
from enable.component_editor import ComponentEditor
 
17
from traits.api import HasTraits, Instance, DelegatesTo
 
18
from traitsui.api import View, Item, HGroup, VGroup
 
19
 
 
20
 
 
21
class CursorTest(HasTraits):
 
22
    plot = Instance(HPlotContainer)
 
23
    cursor1 = Instance(BaseCursorTool)
 
24
    cursor2 = Instance(BaseCursorTool)
 
25
 
 
26
    cursor1pos = DelegatesTo('cursor1', prefix='current_position')
 
27
    cursor2pos = DelegatesTo('cursor2', prefix='current_position')
 
28
 
 
29
    def __init__(self):
 
30
        #The delegates views don't work unless we caller the superclass __init__
 
31
        super(CursorTest, self).__init__()
 
32
 
 
33
        container = HPlotContainer(padding=0, spacing=20)
 
34
        self.plot = container
 
35
        #a subcontainer for the first plot.
 
36
        #I'm not sure why this is required. Without it, the layout doesn't work right.
 
37
        subcontainer = OverlayPlotContainer(padding=40)
 
38
        container.add(subcontainer)
 
39
 
 
40
        #make some data
 
41
        index = numpy.linspace(-10,10,512)
 
42
        value = numpy.sin(index)
 
43
 
 
44
        #create a LinePlot instance and add it to the subcontainer
 
45
        line = create_line_plot([index, value], add_grid=True,
 
46
                                add_axis=True, index_sort='ascending',
 
47
                                orientation = 'h')
 
48
        subcontainer.add(line)
 
49
 
 
50
        #here's our first cursor.
 
51
        csr = CursorTool(line,
 
52
                        drag_button="left",
 
53
                        color='blue')
 
54
        self.cursor1 = csr
 
55
        #and set it's initial position (in data-space units)
 
56
        csr.current_position = 0.0, 0.0
 
57
 
 
58
        #this is a rendered component so it goes in the overlays list
 
59
        line.overlays.append(csr)
 
60
 
 
61
        #some other standard tools
 
62
        line.tools.append(PanTool(line, drag_button="right"))
 
63
        line.overlays.append(ZoomTool(line))
 
64
 
 
65
        #make some 2D data for a colourmap plot
 
66
        xy_range = (-5, 5)
 
67
        x = numpy.linspace(xy_range[0], xy_range[1] ,100)
 
68
        y = numpy.linspace(xy_range[0], xy_range[1] ,100)
 
69
        X,Y = numpy.meshgrid(x, y)
 
70
        Z = numpy.sin(X)*numpy.arctan2(Y,X)
 
71
 
 
72
        #easiest way to get a CMapImagePlot is to use the Plot class
 
73
        ds = ArrayPlotData()
 
74
        ds.set_data('img', Z)
 
75
 
 
76
        img = Plot(ds, padding=40)
 
77
        cmapImgPlot = img.img_plot("img",
 
78
                     xbounds = xy_range,
 
79
                     ybounds = xy_range,
 
80
                     colormap = jet)[0]
 
81
 
 
82
        container.add(img)
 
83
 
 
84
        #now make another cursor
 
85
        csr2 = CursorTool(cmapImgPlot,
 
86
                           drag_button='left',
 
87
                           color='white',
 
88
                           line_width=2.0
 
89
                           )
 
90
        self.cursor2 = csr2
 
91
 
 
92
        csr2.current_position = 1.0, 1.5
 
93
 
 
94
        cmapImgPlot.overlays.append(csr2)
 
95
 
 
96
        #add some standard tools. Note, I'm assigning the PanTool to the
 
97
        #right mouse-button to avoid conflicting with the cursors
 
98
        cmapImgPlot.tools.append(PanTool(cmapImgPlot, drag_button="right"))
 
99
        cmapImgPlot.overlays.append(ZoomTool(cmapImgPlot))
 
100
 
 
101
 
 
102
    traits_view = View(VGroup(
 
103
                            HGroup(Item('plot',
 
104
                                        editor=ComponentEditor(),
 
105
                                        resizable=True, springy=True,
 
106
                                        show_label=False),
 
107
                                        springy=True),
 
108
                            HGroup(Item('cursor1pos', width=300),
 
109
                                   Item('cursor2pos', width=300))),
 
110
                        title="Cursor Tool Demo",
 
111
                        resizable=True,
 
112
                        width=800,
 
113
                        height=420)
 
114
 
 
115
#===============================================================================
 
116
# # demo object that is used by the demo.py application.
 
117
#===============================================================================
 
118
demo = CursorTest()
 
119
 
 
120
if __name__=='__main__':
 
121
    demo.configure_traits()
 
122