4
Demonstrates using a line segment drawing tool on top of the
5
scatter plot from simple_scatter.py.
8
Zoom interactions are the same as in simple_line.py.
11
- left click places a new point
12
- moving over an existing point and left-dragging will reposition that point
13
- moving over an existing point and ctrl-left-clicking will delete that point
14
- pressing "Enter" will "finalize" the selection. This means that the
15
tool's _finalize_selection() method will be called, and the list of
16
drawn points will be reset. By default, _finalize_selection() does nothing,
17
but subclasses can customize this.
20
# Major library imports
21
from numpy import sort
22
from numpy.random import random
24
# Enthought library imports
25
from enable.api import Component, ComponentEditor
26
from traits.api import HasTraits, Instance
27
from traitsui.api import Item, Group, View
30
from chaco.api import ArrayPlotData, Plot
31
from chaco.tools.api import LineSegmentTool, PanTool, ZoomTool
34
class MyLineDrawer(LineSegmentTool):
36
This class demonstrates how to customize the behavior of the
37
LineSegmentTool via subclassing.
40
def _finalize_selection(self):
41
print "Dataspace points:"
42
for point in self.points:
45
#===============================================================================
46
# # Create the Chaco plot.
47
#===============================================================================
48
def _create_plot_component():
52
x = sort(random(numpts))
55
# Create a plot data obect and give it this data
57
pd.set_data("index", x)
58
pd.set_data("value", y)
62
plot.plot(("index", "value"),
66
index_sort="ascending",
72
# Tweak some of the plot properties
73
plot.title = "Click to add points, press Enter to finalize selection"
77
# Attach some tools to the plot
78
pan = PanTool(plot, drag_button="right", constrain_key="shift")
79
plot.tools.append(pan)
80
zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
81
plot.overlays.append(zoom)
82
plot.overlays.append(MyLineDrawer(plot))
85
#===============================================================================
86
# Attributes to use for the plot view.
88
title="Line drawing example"
91
#===============================================================================
92
# # Demo class that is used by the demo.py application.
93
#===============================================================================
94
class Demo(HasTraits):
95
plot = Instance(Component)
99
Item('plot', editor=ComponentEditor(size=size,
102
orientation = "vertical"),
103
resizable=True, title=title
106
def _plot_default(self):
107
return _create_plot_component()
111
if __name__ == "__main__":
112
demo.configure_traits()