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 enthought.chaco.example_support import COLOR_PALETTE
24
from enthought.enable.example_support import DemoFrame, demo_main
26
# Enthought library imports
27
from enthought.enable.api import Window
30
from enthought.chaco.api import create_line_plot, OverlayPlotContainer, PlotLabel, \
31
create_scatter_plot, Legend, PlotGrid
32
from enthought.chaco.tools.api import PanTool, ZoomTool, \
33
LegendTool, TraitsTool
35
from enthought.chaco.scales.api import CalendarScaleSystem
36
from enthought.chaco.scales_tick_generator import ScalesTickGenerator
37
from enthought.chaco.scales_axis import PlotAxis
39
def add_default_axes(plot, orientation="normal", vtitle="",htitle=""):
41
Creates left and bottom axes for a plot. Assumes that the index is
42
horizontal and value is vertical by default; set orientation to
43
something other than "normal" if they are flipped.
45
if orientation in ("normal", "h"):
46
v_mapper = plot.value_mapper
47
h_mapper = plot.index_mapper
49
v_mapper = plot.index_mapper
50
h_mapper = plot.value_mapper
52
left = PlotAxis(orientation='left',
57
bottom = PlotAxis(orientation='bottom',
62
plot.underlays.append(left)
63
plot.underlays.append(bottom)
67
def add_default_grids(plot, orientation="normal", tick_gen=None):
69
Creates horizontal and vertical gridlines for a plot. Assumes that the
70
index is horizontal and value is vertical by default; set orientation to
71
something other than "normal" if they are flipped.
73
if orientation in ("normal", "h"):
74
v_mapper = plot.index_mapper
75
h_mapper = plot.value_mapper
77
v_mapper = plot.value_mapper
78
h_mapper = plot.index_mapper
80
vgrid = PlotGrid(mapper=v_mapper, orientation='vertical',
82
line_color="lightgray", line_style="dot",
83
tick_generator = tick_gen)
85
hgrid = PlotGrid(mapper=h_mapper, orientation='horizontal',
87
line_color="lightgray", line_style="dot",
88
tick_generator = ScalesTickGenerator())
90
plot.underlays.append(vgrid)
91
plot.underlays.append(hgrid)
95
class PlotFrame(DemoFrame):
96
def _create_window(self):
97
container = OverlayPlotContainer(padding = 50, fill_padding = True,
98
bgcolor = "lightgray", use_backbuffer=True)
99
self.container = container
101
# Create the initial X-series of data
105
x = linspace(low, high, numpoints)
108
timex = linspace(now, now+7*24*3600, numpoints)
110
# Plot some bessel functions
117
plot = create_line_plot((timex,y), color=tuple(COLOR_PALETTE[i]), width=2.0)
118
plot.index.sort_order = "ascending"
120
plot = create_scatter_plot((timex,y), color=tuple(COLOR_PALETTE[i]))
121
plot.bgcolor = "white"
122
plot.border_visible = True
124
value_mapper = plot.value_mapper
125
index_mapper = plot.index_mapper
126
left, bottom = add_default_axes(plot)
127
left.tick_generator = ScalesTickGenerator()
128
bottom.tick_generator = ScalesTickGenerator(scale=CalendarScaleSystem())
129
add_default_grids(plot, tick_gen=bottom.tick_generator)
131
plot.value_mapper = value_mapper
132
value_mapper.range.add(plot.value)
133
plot.index_mapper = index_mapper
134
index_mapper.range.add(plot.index)
137
plot.tools.append(PanTool(plot))
138
zoom = ZoomTool(plot, tool_mode="box", always_on=False)
139
plot.overlays.append(zoom)
140
# Add a legend in the upper right corner, and make it relocatable
141
legend = Legend(component=plot, padding=10, align="ur")
142
legend.tools.append(LegendTool(legend, drag_button="right"))
143
plot.overlays.append(legend)
146
plots["Bessel j_%d"%i] = plot
148
# Set the list of plots on the legend
151
# Add the title at the top
152
container.overlays.append(PlotLabel("Bessel functions",
155
overlay_position="top"))
157
# Add the traits inspector tool to the container
158
container.tools.append(TraitsTool(container))
160
return Window(self, -1, component=container)
162
if __name__ == "__main__":
163
demo_main(PlotFrame, size=(800,700), title="Simple line plot")