3
Scatter plot with point selection
5
Draws a simple scatter plot of random data. The user can click on points to
6
select or unselect them.
8
- Left-click on a point to select or unselect it.
13
# FIXME: the 'z' zoom interaction is ill-behaved.
15
# Major library imports
16
from numpy import sort
17
from numpy.random import random
19
# Enthought library imports
20
from enable.api import Component, ComponentEditor
21
from traits.api import HasTraits, Instance
22
from traitsui.api import Item, VGroup, View, Label, HGroup, spring
25
from chaco.api import AbstractDataSource, ArrayPlotData, Plot, \
26
ScatterInspectorOverlay
27
from chaco.tools.api import ScatterInspector, PanTool, ZoomTool
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 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
my_plot.tools.append(ScatterInspector(my_plot, selection_mode="toggle",
66
persistent_hover=False))
67
my_plot.overlays.append(
68
ScatterInspectorOverlay(my_plot,
69
hover_color = "transparent",
70
hover_marker_size = 10,
71
hover_outline_color = "purple",
73
selection_marker_size = 8,
74
selection_color = "lawngreen")
77
my_plot.tools.append(PanTool(my_plot))
78
my_plot.overlays.append(ZoomTool(my_plot, drag_button="right"))
82
#===============================================================================
83
# Attributes to use for the plot view.
85
title="Scatter plot with selection"
88
#===============================================================================
89
# # Demo class that is used by the demo.py application.
90
#===============================================================================
91
class Demo(HasTraits):
92
plot = Instance(Component)
96
HGroup(spring, Label('Click point to select/unselect'),
98
Item('plot', editor=ComponentEditor(size=size,
101
orientation = "vertical"),
102
resizable=True, title=title
105
def _metadata_handler(self):
106
sel_indices = self.index_datasource.metadata.get('selections', [])
107
print "Selection indices:", sel_indices
109
hover_indices = self.index_datasource.metadata.get('hover', [])
110
print "Hover indices:", hover_indices
112
def _plot_default(self):
113
plot = _create_plot_component()
115
# Retrieve the plot hooked to the tool.
116
my_plot = plot.plots["my_plot"][0]
118
# Set up the trait handler for the selection
119
self.index_datasource = my_plot.index
120
self.index_datasource.on_trait_change(self._metadata_handler,
127
if __name__ == "__main__":
128
demo.configure_traits()