3
Demonstrates the LassoTool and overlay on a colormapped image
4
plot. The underlying plot is similar to the one in cmap_image_plot.py.
6
Use Shift-drag to select multiple disjoint regions.
9
# Major library imports
10
from numpy import linspace, meshgrid, pi, sin
12
# Enthought library imports
13
from enable.api import Component, ComponentEditor
14
from traits.api import HasTraits, Instance
15
from traitsui.api import Item, Group, View
18
from chaco.api import ArrayPlotData, jet, Plot, LassoOverlay
19
from chaco.tools.api import LassoSelection, LassoSelection
21
#===============================================================================
22
# # Create the Chaco plot.
23
#===============================================================================
25
def lasso_updated(lasso_tool, name, old, new_selections):
26
# new_selections is a list of arrays of coordinates in dataspace. It is a
27
# list because the LassoSelection supports multiple, disjoint selection regions.
28
for i, selection in enumerate(new_selections):
29
print "Selection region", i
31
# We first map to screen because the selection is stored as coordinates
33
screen_pts = lasso_tool.plot.map_screen(selection)
35
# Now map each point into the grid index
36
for x, y in screen_pts:
37
print "\t", lasso_tool.plot.map_index((x, y))
40
def _create_plot_component():# Create a scalar field to colormap
41
xbounds = (-2*pi, 2*pi, 600)
42
ybounds = (-1.5*pi, 1.5*pi, 300)
43
xs = linspace(*xbounds)
44
ys = linspace(*ybounds)
45
x, y = meshgrid(xs,ys)
48
# Create a plot data obect and give it this data
50
pd.set_data("imagedata", z)
54
img_plot = plot.img_plot("imagedata",
59
# Tweak some of the plot properties
60
plot.title = "Image Plot with Lasso"
63
lasso_selection = LassoSelection(component=img_plot)
64
lasso_selection.on_trait_change(lasso_updated, "disjoint_selections")
65
lasso_overlay = LassoOverlay(lasso_selection = lasso_selection, component=img_plot)
66
img_plot.tools.append(lasso_selection)
67
img_plot.overlays.append(lasso_overlay)
70
#===============================================================================
71
# Attributes to use for the plot view.
73
title="Image Plot with Lasso"
75
#===============================================================================
76
# # Demo class that is used by the demo.py application.
77
#===============================================================================
78
class Demo(HasTraits):
79
plot = Instance(Component)
83
Item('plot', editor=ComponentEditor(size=size),
85
orientation = "vertical"),
86
resizable=True, title=title
89
def _plot_default(self):
90
return _create_plot_component()
94
if __name__ == "__main__":
95
demo.configure_traits()