2
Draws a scatterplot of a set of random points of variable size.
3
- This uses the non-standard renderer, VariableSizeScatterPlot
4
- Left-drag pans the plot.
5
- Mousewheel up and down zooms the plot in and out.
6
- Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular
7
region to zoom. If you use a sequence of zoom boxes, pressing alt-left-arrow
8
and alt-right-arrow moves you forwards and backwards through the "zoom
12
# Major library imports
16
# Enthought library imports
17
from enable.api import Component, ComponentEditor
18
from traits.api import HasTraits, Instance
19
from traitsui.api import Item, Group, View
22
from chaco.api import ArrayPlotData, Plot, VariableSizeScatterPlot, \
23
LinearMapper, ArrayDataSource
24
from chaco.tools.api import PanTool, ZoomTool
26
#===============================================================================
27
# # Create the Chaco plot.
28
#===============================================================================
29
def _create_plot_component():
33
x = numpy.arange(0, numpts)
34
y = numpy.random.random(numpts)
35
marker_size = numpy.random.normal(4.0, 4.0, numpts)
37
# Create a plot data object and give it this data
39
pd.set_data("index", x)
40
pd.set_data("value", y)
42
# Because this is a non-standard renderer, we can't call plot.plot, which
43
# sets up the array data sources, mappers and default index/value ranges.
44
# So, its gotta be done manually for now.
46
index_ds = ArrayDataSource(x)
47
value_ds = ArrayDataSource(y)
51
plot.index_range.add(index_ds)
52
plot.value_range.add(value_ds)
54
# Create the index and value mappers using the plot data ranges
55
imapper = LinearMapper(range=plot.index_range)
56
vmapper = LinearMapper(range=plot.value_range)
58
# Create the scatter renderer
59
scatter = VariableSizeScatterPlot(
62
index_mapper = imapper,
63
value_mapper = vmapper,
65
marker_size=marker_size,
66
color=(1.0,0.0,0.75,0.4))
68
# Append the renderer to the list of the plot's plots
70
plot.plots['var_size_scatter'] = [scatter]
72
# Tweak some of the plot properties
73
plot.title = "Scatter Plot"
77
# Attach some tools to the plot
78
plot.tools.append(PanTool(plot, constrain_key="shift"))
79
zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
80
plot.overlays.append(zoom)
84
#===============================================================================
85
# Attributes to use for the plot view.
87
title = "Basic scatter plot"
90
#===============================================================================
91
# # Demo class that is used by the demo.py application.
92
#===============================================================================
93
class Demo(HasTraits):
94
plot = Instance(Component)
98
Item('plot', editor=ComponentEditor(size=size,
101
orientation = "vertical"),
102
resizable=True, title=title
105
def _plot_default(self):
106
return _create_plot_component()
110
if __name__ == "__main__":
111
demo.configure_traits()