~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to examples/demo/basic/tabbed_plots.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2014-06-01 17:04:08 UTC
  • mfrom: (7.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140601170408-m86xvdjd83a4qon0
Tags: 4.4.1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
 - Let the binary-predeb target work on the usr/lib/python* directory
   as we don't have usr/share/pyshared anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Tabbed plots with linked (shared) data ranges
3
 
 
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.
7
 
 
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.
10
 
 
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. 
13
 
 
14
 
Mousewheel zooms in or out. Left-mouse-drag pans. 
15
 
Typing "z", then left-mouse-drag, zooms to a specified region.
16
 
 
17
 
If you zoom or pan one plot, you will see changes in one or both axis ranges
18
 
of the other plots.
19
 
"""
20
 
 
21
 
from numpy import linspace, pi, sin, tan, cos
22
 
 
23
 
from traits.api import HasTraits, Instance
24
 
from traitsui.api import UItem, Tabbed, View, VGroup
25
 
 
26
 
from chaco.api import Plot, AbstractPlotData, ArrayPlotData
27
 
from chaco.tools.api import PanTool, ZoomTool
28
 
from enable.component_editor import ComponentEditor
29
 
 
30
 
 
31
 
class TabbedPlots(HasTraits):
32
 
 
33
 
    data = Instance(AbstractPlotData)
34
 
 
35
 
    plot_sin = Instance(Plot)
36
 
    plot_tan = Instance(Plot)
37
 
    plot_mixed = Instance(Plot)
38
 
 
39
 
    view = View(
40
 
        VGroup(
41
 
            # UItem is an unlabeled item
42
 
            UItem('plot_sin', editor=ComponentEditor(), dock='tab'),
43
 
            Tabbed(
44
 
                UItem('plot_tan', editor=ComponentEditor(), dock='tab'),
45
 
                UItem('plot_mixed', editor=ComponentEditor(), dock='tab'))
46
 
            ),
47
 
        title='Tabbed plots with shared data ranges',
48
 
        width=0.67,
49
 
        height=0.4,
50
 
        resizable=True
51
 
    )
52
 
 
53
 
    def create_plot(self, data, name, color):
54
 
        p = Plot(self.data)
55
 
        p.plot(data, name=name, color=color)
56
 
        p.tools.append(PanTool(p))
57
 
        p.overlays.append(ZoomTool(p))
58
 
        return p
59
 
 
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")
64
 
 
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
70
 
        
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
77
 
 
78
 
    def _data_changed(self):
79
 
        self.create_plots()
80
 
 
81
 
 
82
 
#===============================================================================
83
 
# # demo object that is used by the demo.py application.
84
 
#===============================================================================
85
 
x = linspace(-2*pi, 2*pi, 100)
86
 
demo = TabbedPlots(
87
 
            data = ArrayPlotData(x=x, 
88
 
                                 ysin=sin(x), 
89
 
                                 ytan=tan(x),
90
 
                                 ymix=sin(x)**2 + cos(x)))
91
 
 
92
 
if __name__ == "__main__":
93
 
    demo.configure_traits()