2
A Demonstration of the CursorTool functionality
4
Left-button drag to move the cursors round.
5
Right-drag to pan the plots. 'z'-key to Zoom
8
# Major library imports
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
21
class CursorTest(HasTraits):
22
plot = Instance(HPlotContainer)
23
cursor1 = Instance(BaseCursorTool)
24
cursor2 = Instance(BaseCursorTool)
26
cursor1pos = DelegatesTo('cursor1', prefix='current_position')
27
cursor2pos = DelegatesTo('cursor2', prefix='current_position')
30
#The delegates views don't work unless we caller the superclass __init__
31
super(CursorTest, self).__init__()
33
container = HPlotContainer(padding=0, spacing=20)
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)
41
index = numpy.linspace(-10,10,512)
42
value = numpy.sin(index)
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',
48
subcontainer.add(line)
50
#here's our first cursor.
51
csr = CursorTool(line,
55
#and set it's initial position (in data-space units)
56
csr.current_position = 0.0, 0.0
58
#this is a rendered component so it goes in the overlays list
59
line.overlays.append(csr)
61
#some other standard tools
62
line.tools.append(PanTool(line, drag_button="right"))
63
line.overlays.append(ZoomTool(line))
65
#make some 2D data for a colourmap plot
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)
72
#easiest way to get a CMapImagePlot is to use the Plot class
76
img = Plot(ds, padding=40)
77
cmapImgPlot = img.img_plot("img",
84
#now make another cursor
85
csr2 = CursorTool(cmapImgPlot,
92
csr2.current_position = 1.0, 1.5
94
cmapImgPlot.overlays.append(csr2)
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))
102
traits_view = View(VGroup(
104
editor=ComponentEditor(),
105
resizable=True, springy=True,
108
HGroup(Item('cursor1pos', width=300),
109
Item('cursor2pos', width=300))),
110
title="Cursor Tool Demo",
115
#===============================================================================
116
# # demo object that is used by the demo.py application.
117
#===============================================================================
120
if __name__=='__main__':
121
demo.configure_traits()