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

« back to all changes in this revision

Viewing changes to examples/demo/simple_line.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
 
Draw overlapping line plots (Bessel functions)
3
 
 
4
 
Draws overlapping line plots with legends. Some are drawn as lines, 
5
 
some as points only.
6
 
 
7
 
Left-drag pans the plot.
8
 
 
9
 
Right-drag (in the Y direction) zooms the plot in and out.
10
 
 
11
 
Mousewheel up and down zooms the plot in and out.
12
 
 
13
 
Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular
14
 
region to zoom. 
15
 
 
16
 
Right-drag on the legend allows you to reposition it.
17
 
"""
18
 
 
19
 
# Major library imports
20
 
from numpy import arange
21
 
from scipy.special import jn
22
 
 
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
27
 
 
28
 
# Chaco imports
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
33
 
 
34
 
 
35
 
class OverlappingPlotContainer(OverlayPlotContainer):
36
 
    """Simple container for creating a series of plots"""
37
 
 
38
 
    numpoints = Int(100)
39
 
    low = Float(-5.0)
40
 
    high = Float(15.0)
41
 
    num_funs = Int(10)
42
 
 
43
 
    def __init__(self, *args, **kws):
44
 
        super(OverlayPlotContainer, self).__init__(*args, **kws)
45
 
        self._setup_plots()
46
 
 
47
 
    def _setup_plots(self):
48
 
        """Creates series of Bessel function plots"""
49
 
        plots = {}
50
 
        x = arange(self.low, self.high + 0.001,
51
 
                   (self.high - self.low) / self.numpoints)
52
 
 
53
 
        for i in range(self.num_funs):
54
 
            y = jn(i, x)
55
 
            if i % 2 == 1:
56
 
                plot = create_line_plot((x, y),
57
 
                                        color=tuple(COLOR_PALETTE[i]),
58
 
                                        width=2.0)
59
 
            else:
60
 
                plot = create_scatter_plot((x, y),
61
 
                                            color=tuple(COLOR_PALETTE[i]))
62
 
 
63
 
            if i == 0:
64
 
                value_mapper, index_mapper, legend = \
65
 
                    self._setup_plot_tools(plot)
66
 
            else:
67
 
                self._setup_mapper(plot, value_mapper, index_mapper)
68
 
 
69
 
            self.add(plot)
70
 
            plots["Bessel j_%d" % i] = plot
71
 
 
72
 
        # Set the list of plots on the legend
73
 
        legend.plots = plots
74
 
 
75
 
        # Add the title at the top
76
 
        self.overlays.append(PlotLabel("Bessel functions",
77
 
                                       component=self,
78
 
                                       font="swiss 16",
79
 
                                       overlay_position="top"))
80
 
 
81
 
        # Add the traits inspector tool to the container
82
 
        self.tools.append(TraitsTool(self))
83
 
 
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)
90
 
 
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()
96
 
 
97
 
        # The PanTool allows panning around the plot
98
 
        plot.tools.append(PanTool(plot))
99
 
 
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)
104
 
 
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)
109
 
 
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)
114
 
 
115
 
        return plot.value_mapper, plot.index_mapper, legend
116
 
 
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)
123
 
 
124
 
 
125
 
size = (800, 700)
126
 
title = "Simple Line Plot"
127
 
 
128
 
 
129
 
class PlotExample(HasTraits):
130
 
    plot = Instance(Component)
131
 
 
132
 
    traits_view = View(
133
 
                    Group(
134
 
                        Item('plot', editor=ComponentEditor(size=size),
135
 
                             show_label=False),
136
 
                        orientation="vertical"),
137
 
                    resizable=True, title=title,
138
 
                    width=size[0], height=size[1]
139
 
                    )
140
 
 
141
 
    def _plot_default(self):
142
 
        return OverlappingPlotContainer(padding=50, fill_padding=True,
143
 
                                     bgcolor="lightgray", use_backbuffer=True)
144
 
 
145
 
 
146
 
demo = PlotExample()
147
 
 
148
 
 
149
 
if __name__ == "__main__":
150
 
    demo.configure_traits()