2
Lasso selection of data points
4
Draws a simple scatterplot of random data. Drag the mouse to use the lasso
5
selector, which allows you to circle all the points in a region.
7
Upon completion of the lasso operation, the indices of the selected points are
8
printed to the console.
10
Uncomment 'lasso_selection.incremental_select' line (line 74) to see the
11
indices of the selected points computed in real time.
16
# Major library imports
17
from numpy import sort, compress, arange
18
from numpy.random import random
20
# Enthought library imports
21
from enable.api import Component, ComponentEditor
22
from traits.api import HasTraits, Instance
23
from traitsui.api import Item, Group, View
26
from chaco.api import ArrayPlotData, Plot, LassoOverlay
27
from chaco.tools.api import LassoSelection, ScatterInspector
29
#===============================================================================
30
# # Create the Chaco plot.
31
#===============================================================================
32
def _create_plot_component():
36
x = sort(random(npts))
39
# Create a plot data obect and give it this data
41
pd.set_data("index", x)
42
pd.set_data("value", y)
46
plot.plot(("index", "value"),
50
index_sort="ascending",
55
# Tweak some of the plot properties
56
plot.title = "Scatter Plot With Lasso Selection"
60
# Right now, some of the tools are a little invasive, and we need the
61
# actual ScatterPlot object to give to them
62
my_plot = plot.plots["my_plot"][0]
64
# Attach some tools to the plot
65
lasso_selection = LassoSelection(component=my_plot,
66
selection_datasource=my_plot.index)
67
my_plot.active_tool = lasso_selection
68
my_plot.tools.append(ScatterInspector(my_plot))
69
lasso_overlay = LassoOverlay(lasso_selection=lasso_selection,
71
my_plot.overlays.append(lasso_overlay)
73
# Uncomment this if you would like to see incremental updates:
74
#lasso_selection.incremental_select = True
79
#===============================================================================
80
# Attributes to use for the plot view.
82
title="Scatter plot with selection"
85
#===============================================================================
86
# # Demo class that is used by the demo.py application.
87
#===============================================================================
88
class Demo(HasTraits):
89
plot = Instance(Component)
93
Item('plot', editor=ComponentEditor(size=size),
95
orientation = "vertical"),
96
resizable=True, title=title
99
def _selection_changed(self):
100
mask = self.index_datasource.metadata['selection']
101
print "New selection: "
102
print compress(mask, arange(len(mask)))
103
# Ensure that the points are printed immediately:
107
def _plot_default(self):
108
plot = _create_plot_component()
110
# Retrieve the plot hooked to the LassoSelection tool.
111
my_plot = plot.plots["my_plot"][0]
112
lasso_selection = my_plot.active_tool
114
# Set up the trait handler for the selection
115
self.index_datasource = my_plot.index
116
lasso_selection.on_trait_change(self._selection_changed,
123
if __name__ == "__main__":
124
demo.configure_traits()