3
Draws several overlapping line plots.
5
Left-drag pans the plot.
7
Mousewheel up and down zooms the plot in and out.
9
Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular region to
10
zoom. If you use a sequence of zoom boxes, pressing alt-left-arrow and
11
alt-right-arrow moves you forwards and backwards through the "zoom history".
13
Right-click and dragging on the legend allows you to reposition the legend.
15
Double-clicking on line or scatter plots brings up a traits editor for the plot.
18
# Major library imports
19
from numpy import linspace
20
from scipy.special import jn
23
from chaco.example_support import COLOR_PALETTE
24
# Enthought library imports
25
from enable.api import Component, ComponentEditor
26
from traits.api import HasTraits, Instance
27
from traitsui.api import Item, Group, View
30
from chaco.api import create_line_plot, OverlayPlotContainer, PlotLabel, \
31
create_scatter_plot, Legend, PlotGrid
32
from chaco.tools.api import PanTool, ZoomTool, \
33
LegendTool, TraitsTool
35
from chaco.scales.api import CalendarScaleSystem
36
from chaco.scales_tick_generator import ScalesTickGenerator
37
from chaco.axis import PlotAxis
39
#===============================================================================
40
# # Create the Chaco plot.
41
#===============================================================================
43
def add_default_axes(plot, orientation="normal", vtitle="",htitle=""):
45
Creates left and bottom axes for a plot. Assumes that the index is
46
horizontal and value is vertical by default; set orientation to
47
something other than "normal" if they are flipped.
49
if orientation in ("normal", "h"):
50
v_mapper = plot.value_mapper
51
h_mapper = plot.index_mapper
53
v_mapper = plot.index_mapper
54
h_mapper = plot.value_mapper
56
left = PlotAxis(orientation='left',
61
bottom = PlotAxis(orientation='bottom',
66
plot.underlays.append(left)
67
plot.underlays.append(bottom)
71
def add_default_grids(plot, orientation="normal", tick_gen=None):
73
Creates horizontal and vertical gridlines for a plot. Assumes that the
74
index is horizontal and value is vertical by default; set orientation to
75
something other than "normal" if they are flipped.
77
if orientation in ("normal", "h"):
78
v_mapper = plot.index_mapper
79
h_mapper = plot.value_mapper
81
v_mapper = plot.value_mapper
82
h_mapper = plot.index_mapper
84
vgrid = PlotGrid(mapper=v_mapper, orientation='vertical',
86
line_color="lightgray", line_style="dot",
87
tick_generator = tick_gen)
89
hgrid = PlotGrid(mapper=h_mapper, orientation='horizontal',
91
line_color="lightgray", line_style="dot",
92
tick_generator = ScalesTickGenerator())
94
plot.underlays.append(vgrid)
95
plot.underlays.append(hgrid)
98
def _create_plot_component():
99
container = OverlayPlotContainer(padding = 50, fill_padding = True,
100
bgcolor = "lightgray", use_backbuffer=True)
102
# Create the initial X-series of data
106
x = linspace(low, high, numpoints)
109
timex = linspace(now, now+7*24*3600, numpoints)
111
# Plot some bessel functions
118
plot = create_line_plot((timex,y), color=tuple(COLOR_PALETTE[i]), width=2.0)
119
plot.index.sort_order = "ascending"
121
plot = create_scatter_plot((timex,y), color=tuple(COLOR_PALETTE[i]))
122
plot.bgcolor = "white"
123
plot.border_visible = True
125
value_mapper = plot.value_mapper
126
index_mapper = plot.index_mapper
127
left, bottom = add_default_axes(plot)
128
left.tick_generator = ScalesTickGenerator()
129
bottom.tick_generator = ScalesTickGenerator(scale=CalendarScaleSystem())
130
add_default_grids(plot, tick_gen=bottom.tick_generator)
132
plot.value_mapper = value_mapper
133
value_mapper.range.add(plot.value)
134
plot.index_mapper = index_mapper
135
index_mapper.range.add(plot.index)
138
plot.tools.append(PanTool(plot))
139
zoom = ZoomTool(plot, tool_mode="box", always_on=False)
140
plot.overlays.append(zoom)
141
# Add a legend in the upper right corner, and make it relocatable
142
legend = Legend(component=plot, padding=10, align="ur")
143
legend.tools.append(LegendTool(legend, drag_button="right"))
144
plot.overlays.append(legend)
147
plots["Bessel j_%d"%i] = plot
149
# Set the list of plots on the legend
152
# Add the title at the top
153
container.overlays.append(PlotLabel("Bessel functions",
156
overlay_position="top"))
158
# Add the traits inspector tool to the container
159
container.tools.append(TraitsTool(container))
163
#===============================================================================
164
# Attributes to use for the plot view.
166
title="Simple line plot"
168
#===============================================================================
169
# # Demo class that is used by the demo.py application.
170
#===============================================================================
171
class Demo(HasTraits):
172
plot = Instance(Component)
176
Item('plot', editor=ComponentEditor(size=size),
178
orientation = "vertical"),
179
resizable=True, title=title,
180
width=size[0], height=size[1]
183
def _plot_default(self):
184
return _create_plot_component()
188
if __name__ == "__main__":
189
demo.configure_traits()