2
Draw overlapping line plots (Bessel functions)
4
Draws overlapping line plots with legends. Some are drawn as lines,
7
Left-drag pans the plot.
9
Right-drag (in the Y direction) zooms the plot in and out.
11
Mousewheel up and down zooms the plot in and out.
13
Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular
16
Right-drag on the legend allows you to reposition it.
19
# Major library imports
20
from numpy import arange
21
from scipy.special import jn
23
# Enthought library imports
24
from enable.api import Component, ComponentEditor
25
from traits.api import Float, HasTraits, Int, Instance
26
from traitsui.api import Item, Group, View
29
from chaco.api import create_line_plot, add_default_axes, add_default_grids, \
30
OverlayPlotContainer, PlotLabel, create_scatter_plot, Legend
31
from chaco.tools.api import PanTool, ZoomTool, LegendTool, TraitsTool, DragZoom
32
from chaco.example_support import COLOR_PALETTE
35
class OverlappingPlotContainer(OverlayPlotContainer):
36
"""Simple container for creating a series of plots"""
43
def __init__(self, *args, **kws):
44
super(OverlayPlotContainer, self).__init__(*args, **kws)
47
def _setup_plots(self):
48
"""Creates series of Bessel function plots"""
50
x = arange(self.low, self.high + 0.001,
51
(self.high - self.low) / self.numpoints)
53
for i in range(self.num_funs):
56
plot = create_line_plot((x, y),
57
color=tuple(COLOR_PALETTE[i]),
60
plot = create_scatter_plot((x, y),
61
color=tuple(COLOR_PALETTE[i]))
64
value_mapper, index_mapper, legend = \
65
self._setup_plot_tools(plot)
67
self._setup_mapper(plot, value_mapper, index_mapper)
70
plots["Bessel j_%d" % i] = plot
72
# Set the list of plots on the legend
75
# Add the title at the top
76
self.overlays.append(PlotLabel("Bessel functions",
79
overlay_position="top"))
81
# Add the traits inspector tool to the container
82
self.tools.append(TraitsTool(self))
84
def _setup_plot_tools(self, plot):
85
"""Sets up the background, and several tools on a plot"""
86
# Make a white background with grids and axes
87
plot.bgcolor = "white"
88
add_default_grids(plot)
89
add_default_axes(plot)
91
# Allow white space around plot
92
plot.index_range.tight_bounds = False
93
plot.index_range.refresh()
94
plot.value_range.tight_bounds = False
95
plot.value_range.refresh()
97
# The PanTool allows panning around the plot
98
plot.tools.append(PanTool(plot))
100
# The ZoomTool tool is stateful and allows drawing a zoom
101
# box to select a zoom region.
102
zoom = ZoomTool(plot, tool_mode="box", always_on=False)
103
plot.overlays.append(zoom)
105
# The DragZoom tool just zooms in and out as the user drags
106
# the mouse vertically.
107
dragzoom = DragZoom(plot, drag_button="right")
108
plot.tools.append(dragzoom)
110
# Add a legend in the upper right corner, and make it relocatable
111
legend = Legend(component=plot, padding=10, align="ur")
112
legend.tools.append(LegendTool(legend, drag_button="right"))
113
plot.overlays.append(legend)
115
return plot.value_mapper, plot.index_mapper, legend
117
def _setup_mapper(self, plot, value_mapper, index_mapper):
118
"""Sets up a mapper for given plot"""
119
plot.value_mapper = value_mapper
120
value_mapper.range.add(plot.value)
121
plot.index_mapper = index_mapper
122
index_mapper.range.add(plot.index)
126
title = "Simple Line Plot"
129
class PlotExample(HasTraits):
130
plot = Instance(Component)
134
Item('plot', editor=ComponentEditor(size=size),
136
orientation="vertical"),
137
resizable=True, title=title,
138
width=size[0], height=size[1]
141
def _plot_default(self):
142
return OverlappingPlotContainer(padding=50, fill_padding=True,
143
bgcolor="lightgray", use_backbuffer=True)
149
if __name__ == "__main__":
150
demo.configure_traits()