2
Tabbed plots with linked (shared) data ranges
4
Shows how to place plots in separate tabs. Also shows how two plots can
5
dynamically display the same range of data, so that a zoom or pan in one
6
plot will automatically be shown in the other.
8
In this example, the top panel plots a sin function. The bottom panel contains
9
two tabs, plotting a tan function and a mixed trig function respectively.
11
The three plots are linked. The sin and mixed plots share both x- and y-axis
12
data ranges. The tan plot shares only its x-axis with the other two.
14
Mousewheel zooms in or out. Left-mouse-drag pans.
15
Typing "z", then left-mouse-drag, zooms to a specified region.
17
If you zoom or pan one plot, you will see changes in one or both axis ranges
21
from numpy import linspace, pi, sin, tan, cos
23
from traits.api import HasTraits, Instance
24
from traitsui.api import UItem, Tabbed, View, VGroup
26
from chaco.api import Plot, AbstractPlotData, ArrayPlotData
27
from chaco.tools.api import PanTool, ZoomTool
28
from enable.component_editor import ComponentEditor
31
class TabbedPlots(HasTraits):
33
data = Instance(AbstractPlotData)
35
plot_sin = Instance(Plot)
36
plot_tan = Instance(Plot)
37
plot_mixed = Instance(Plot)
41
# UItem is an unlabeled item
42
UItem('plot_sin', editor=ComponentEditor(), dock='tab'),
44
UItem('plot_tan', editor=ComponentEditor(), dock='tab'),
45
UItem('plot_mixed', editor=ComponentEditor(), dock='tab'))
47
title='Tabbed plots with shared data ranges',
53
def create_plot(self, data, name, color):
55
p.plot(data, name=name, color=color)
56
p.tools.append(PanTool(p))
57
p.overlays.append(ZoomTool(p))
60
def create_plots(self):
61
self.plot_sin = self.create_plot(("x", "ysin"), "sin plot", "red")
62
self.plot_tan = self.create_plot(("x", "ytan"), "tan plot", "blue")
63
self.plot_mixed = self.create_plot(("x", "ymix"), "mixed plot", "green")
65
# The mixed plot will share both x and y ranges with the sin plot.
66
# This 2d range is a single object shared by both plots. For its
67
# initial value, we will use the range of the mixed plot, whose y-range
68
# is auto-set to slightly larger than that of the sin plot.
69
self.plot_sin.range2d = self.plot_mixed.range2d
71
# The sin & mixed plots will share only their x range with the tan plot.
72
# Again, this x-axis range is a single object shared by all 3 plots.
73
# It is contained within the 2d range shared by the sin and mixed plots.
74
# (The independent variable, in this case x, is called "index" in chaco.
75
# The dependent variable is called "value".)
76
self.plot_tan.index_range = self.plot_sin.index_range
78
def _data_changed(self):
82
#===============================================================================
83
# # demo object that is used by the demo.py application.
84
#===============================================================================
85
x = linspace(-2*pi, 2*pi, 100)
87
data = ArrayPlotData(x=x,
90
ymix=sin(x)**2 + cos(x)))
92
if __name__ == "__main__":
93
demo.configure_traits()