Created
October 3, 2016 16:40
-
-
Save jvkersch/30cdb80a84d751e7796fb1f299959ea8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mangled version of Chaco's lasso demo that has a LassoSelection that | |
# constrains the selection to be parallel to the coordinate axes. | |
import sys | |
# Major library imports | |
from numpy import sort, compress, arange | |
from numpy.random import random | |
import numpy as np | |
# Enthought library imports | |
from enable.api import Component, ComponentEditor | |
from traits.api import Any, HasTraits, Instance | |
from traitsui.api import Item, Group, View | |
# Chaco imports | |
from chaco.api import ArrayPlotData, Plot, LassoOverlay, jet | |
from chaco.tools.api import LassoSelection, ScatterInspector | |
class MyLassoSelection(LassoSelection): | |
def selecting_mouse_move(self, event): | |
xform = self.component.get_event_transform(event) | |
event.push_transform(xform, caller=self) | |
new_point = self._map_data(np.array((event.x, event.y))) | |
active = self._active_selection | |
if len(active) == 0: | |
self._active_selection = \ | |
self._make_rectangle(new_point, new_point) | |
else: | |
self._active_selection = \ | |
self._make_rectangle(active[0], new_point) | |
self.updated = True | |
if self.incremental_select: | |
self._update_selection() | |
# Report None for the previous selections | |
self.trait_property_changed("disjoint_selections", None) | |
def _make_rectangle(self, p1, p2): | |
x1, y1 = p1 | |
x2, y2 = p2 | |
return np.array([p1, (x1, y2), p2, (x2, y1)]) | |
#=============================================================================== | |
# # Create the Chaco plot. | |
#=============================================================================== | |
def _create_plot_component(): | |
# Create some data | |
npts = 2000 | |
x = sort(random(npts)) | |
y = random(npts) | |
# Create a plot data obect and give it this data | |
pd = ArrayPlotData() | |
pd.set_data("index", x) | |
pd.set_data("value", y) | |
pd.set_data("color", np.full_like(x, 0.2)) | |
# Create the plot | |
plot = Plot(pd) | |
plot.plot(("index", "value", "color"), | |
type="cmap_scatter", | |
name="my_plot", | |
marker="circle", | |
index_sort="ascending", | |
color_mapper=jet, | |
marker_size=4, | |
bgcolor="white") | |
# Tweak some of the plot properties | |
plot.title = "Scatter Plot With Lasso Selection" | |
plot.line_width = 1 | |
plot.padding = 50 | |
# Right now, some of the tools are a little invasive, and we need the | |
# actual ScatterPlot object to give to them | |
my_plot = plot.plots["my_plot"][0] | |
# Attach some tools to the plot | |
lasso_selection = MyLassoSelection(component=my_plot, | |
selection_datasource=my_plot.index, | |
drag_button="left") | |
my_plot.active_tool = lasso_selection | |
my_plot.tools.append(ScatterInspector(my_plot)) | |
lasso_overlay = LassoOverlay(lasso_selection=lasso_selection, | |
component=my_plot) | |
my_plot.overlays.append(lasso_overlay) | |
# Uncomment this if you would like to see incremental updates: | |
#lasso_selection.incremental_select = True | |
return plot, pd | |
#=============================================================================== | |
# Attributes to use for the plot view. | |
size=(650,650) | |
title="Scatter plot with selection" | |
bg_color="lightgray" | |
#=============================================================================== | |
# # Demo class that is used by the demo.py application. | |
#=============================================================================== | |
class Demo(HasTraits): | |
plot = Instance(Component) | |
_pd = Any | |
traits_view = View( | |
Group( | |
Item('plot', editor=ComponentEditor(size=size), | |
show_label=False), | |
orientation = "vertical"), | |
resizable=True, title=title | |
) | |
def _selection_changed(self): | |
mask = self.index_datasource.metadata['selection'] | |
print("New selection: ") | |
print(compress(mask, arange(len(mask)))) | |
# Ensure that the points are printed immediately: | |
sys.stdout.flush() | |
# Update color for selected points. | |
color = self._pd.get_data("color") | |
color[mask] = 0.8 | |
self._pd.set_data("color", color) | |
def _plot_default(self): | |
plot, pd = _create_plot_component() | |
self._pd = pd # HMMM | |
# Retrieve the plot hooked to the LassoSelection tool. | |
my_plot = plot.plots["my_plot"][0] | |
lasso_selection = my_plot.active_tool | |
# Set up the trait handler for the selection | |
self.index_datasource = my_plot.index | |
lasso_selection.on_trait_change(self._selection_changed, | |
'selection_changed') | |
return plot | |
demo = Demo() | |
if __name__ == "__main__": | |
demo.configure_traits() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment