4
A cursor tool lets you drag vertical and horizontal lines whose intersection
5
defines one point on the plot.
7
Left-button drag to move the cursors round.
9
Right-drag to pan the plots. 'z'-key to Zoom
12
# Major library imports
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
25
class CursorTest(HasTraits):
26
plot = Instance(HPlotContainer)
27
cursor1 = Instance(BaseCursorTool)
28
cursor2 = Instance(BaseCursorTool)
30
cursor1pos = DelegatesTo('cursor1', prefix='current_position')
31
cursor2pos = DelegatesTo('cursor2', prefix='current_position')
34
#The delegates views don't work unless we caller the superclass __init__
35
super(CursorTest, self).__init__()
37
container = HPlotContainer(padding=0, spacing=20)
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)
45
index = numpy.linspace(-10,10,512)
46
value = numpy.sin(index)
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',
52
subcontainer.add(line)
54
#here's our first cursor.
55
csr = CursorTool(line,
59
#and set it's initial position (in data-space units)
60
csr.current_position = 0.0, 0.0
62
#this is a rendered component so it goes in the overlays list
63
line.overlays.append(csr)
65
#some other standard tools
66
line.tools.append(PanTool(line, drag_button="right"))
67
line.overlays.append(ZoomTool(line))
69
#make some 2D data for a colourmap plot
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)
76
#easiest way to get a CMapImagePlot is to use the Plot class
80
img = Plot(ds, padding=40)
81
cmapImgPlot = img.img_plot("img",
88
#now make another cursor
89
csr2 = CursorTool(cmapImgPlot,
96
csr2.current_position = 1.0, 1.5
98
cmapImgPlot.overlays.append(csr2)
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))
106
traits_view = View(VGroup(
108
editor=ComponentEditor(),
109
resizable=True, springy=True,
112
HGroup(Item('cursor1pos', width=300),
113
Item('cursor2pos', width=300))),
114
title="Cursor Tool Demo",
119
#===============================================================================
120
# # demo object that is used by the demo.py application.
121
#===============================================================================
124
if __name__=='__main__':
125
demo.configure_traits()