2
Scatterplot with zoomable/pannable colormap
4
Draws a colormapped scatterplot of random data. The colormap is dynamically
7
In addition to normal zooming and panning on the plot, the user can also
8
pan and zoom the colorbar to change the color mapping of the data values.
10
Left click will pan the colorbar's data region. Right-drag will
11
select a zoom range. Mousewheel up and down will zoom in and out on
12
the data bounds of the color bar.
15
# Major library imports
16
from numpy import exp, 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, Group, View
25
from chaco.api import ArrayPlotData, ColorBar, \
27
LinearMapper, Plot, gist_earth
28
from chaco.tools.api import PanTool, ZoomTool
30
#===============================================================================
31
# # Create the Chaco plot.
32
#===============================================================================
33
def _create_plot_component():
37
x = sort(random(numpts))
39
color = exp(-(x**2 + y**2))
41
# Create a plot data obect and give it this data
43
pd.set_data("index", x)
44
pd.set_data("value", y)
45
pd.set_data("color", color)
49
plot.plot(("index", "value", "color"),
52
color_mapper=gist_earth,
56
outline_color = "black",
57
border_visible = True,
60
# Tweak some of the plot properties
61
plot.title = "Colormapped Scatter Plot with Pan/Zoom Color Bar"
63
plot.x_grid.visible = False
64
plot.y_grid.visible = False
65
plot.x_axis.font = "modern 16"
66
plot.y_axis.font = "modern 16"
68
# Add pan and zoom to the plot
69
plot.tools.append(PanTool(plot, constrain_key="shift"))
71
plot.overlays.append(zoom)
73
# Create the colorbar, handing in the appropriate range and colormap
74
colorbar = ColorBar(index_mapper=LinearMapper(range=plot.color_mapper.range),
75
color_mapper=plot.color_mapper,
81
colorbar.padding_top = plot.padding_top
82
colorbar.padding_bottom = plot.padding_bottom
84
# Add pan and zoom tools to the colorbar
85
colorbar.tools.append(PanTool(colorbar, constrain_direction="y", constrain=True))
86
zoom_overlay = ZoomTool(colorbar, axis="index", tool_mode="range",
87
always_on=True, drag_button="right")
88
colorbar.overlays.append(zoom_overlay)
90
# Create a container to position the plot and the colorbar side-by-side
91
container = HPlotContainer(plot, colorbar, use_backbuffer=True, bgcolor="lightgray")
95
#===============================================================================
96
# Attributes to use for the plot view.
98
title="Colormapped scatter plot"
100
#===============================================================================
101
# # Demo class that is used by the demo.py application.
102
#===============================================================================
103
class Demo(HasTraits):
104
plot = Instance(Component)
108
Item('plot', editor=ComponentEditor(size=size),
110
orientation = "vertical"),
111
resizable=True, title=title
114
def _plot_default(self):
115
return _create_plot_component()
119
if __name__ == "__main__":
120
demo.configure_traits()