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

« back to all changes in this revision

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