2
Scatterplot with range-selectable data points
4
Draws a colormapped scatterplot of random data.
6
In addition to normal zooming and panning on the plot, the user can select
7
a range of data values by right-dragging in the color bar.
9
Left-click in the color bar to cancel the range selection.
12
# Major library imports
13
from numpy import exp, sort
14
from numpy.random import random
16
# Enthought library imports
17
from enable.api import Component, ComponentEditor, Window
18
from traits.api import HasTraits, Instance
19
from traitsui.api import Item, VGroup, View, Label
22
from chaco.api import ArrayPlotData, ColorBar, \
23
ColormappedSelectionOverlay, HPlotContainer, \
24
jet, LinearMapper, Plot
25
from chaco.tools.api import PanTool, ZoomTool, RangeSelection, \
28
#===============================================================================
29
# # Create the Chaco plot.
30
#===============================================================================
31
def _create_plot_component():
35
x = sort(random(numpts))
37
color = exp(-(x**2 + y**2))
39
# Create a plot data obect and give it this data
41
pd.set_data("index", x)
42
pd.set_data("value", y)
43
pd.set_data("color", color)
47
plot.plot(("index", "value", "color"),
54
outline_color = "black",
55
border_visible = True,
58
# Tweak some of the plot properties
59
plot.title = "Colormapped Scatter Plot with Range-selectable Data Points"
61
plot.x_grid.visible = False
62
plot.y_grid.visible = False
63
plot.x_axis.font = "modern 16"
64
plot.y_axis.font = "modern 16"
66
# Right now, some of the tools are a little invasive, and we need the
67
# actual ColomappedScatterPlot object to give to them
68
cmap_renderer = plot.plots["my_plot"][0]
70
# Attach some tools to the plot
71
plot.tools.append(PanTool(plot, constrain_key="shift"))
72
zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
73
plot.overlays.append(zoom)
74
selection = ColormappedSelectionOverlay(cmap_renderer, fade_alpha=0.35,
75
selection_type="mask")
76
cmap_renderer.overlays.append(selection)
78
# Create the colorbar, handing in the appropriate range and colormap
79
colorbar = create_colorbar(plot.color_mapper)
80
colorbar.plot = cmap_renderer
81
colorbar.padding_top = plot.padding_top
82
colorbar.padding_bottom = plot.padding_bottom
84
# Create a container to position the plot and the colorbar side-by-side
85
container = HPlotContainer(use_backbuffer = True)
87
container.add(colorbar)
88
container.bgcolor = "lightgray"
91
def create_colorbar(colormap):
92
colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range),
93
color_mapper=colormap,
98
colorbar.tools.append(RangeSelection(component=colorbar))
99
colorbar.overlays.append(RangeSelectionOverlay(component=colorbar,
100
border_color="white",
102
fill_color="lightgray"))
105
#===============================================================================
106
# Attributes to use for the plot view.
108
title="Colormapped scatter plot"
110
#===============================================================================
111
# # Demo class that is used by the demo.py application.
112
#===============================================================================
113
class Demo(HasTraits):
114
plot = Instance(Component)
118
Label('Right-drag on colorbar to select data range'),
119
Item('plot', editor=ComponentEditor(size=size),
125
def _plot_default(self):
126
return _create_plot_component()
130
if __name__ == "__main__":
131
demo.configure_traits()