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

« back to all changes in this revision

Viewing changes to examples/demo/scales_test.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
 
#!/usr/bin/env python
2
 
"""
3
 
Draws several overlapping line plots.
4
 
 
5
 
Left-drag pans the plot.
6
 
 
7
 
Mousewheel up and down zooms the plot in and out.
8
 
 
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".
12
 
 
13
 
Right-click and dragging on the legend allows you to reposition the legend.
14
 
 
15
 
Double-clicking on line or scatter plots brings up a traits editor for the plot.
16
 
"""
17
 
 
18
 
# Major library imports
19
 
from numpy import linspace
20
 
from scipy.special import jn
21
 
from time import time
22
 
 
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
28
 
 
29
 
# Chaco imports
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
34
 
 
35
 
from chaco.scales.api import CalendarScaleSystem
36
 
from chaco.scales_tick_generator import ScalesTickGenerator
37
 
from chaco.axis import PlotAxis
38
 
 
39
 
#===============================================================================
40
 
# # Create the Chaco plot.
41
 
#===============================================================================
42
 
 
43
 
def add_default_axes(plot, orientation="normal", vtitle="",htitle=""):
44
 
    """
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.
48
 
    """
49
 
    if orientation in ("normal", "h"):
50
 
        v_mapper = plot.value_mapper
51
 
        h_mapper = plot.index_mapper
52
 
    else:
53
 
        v_mapper = plot.index_mapper
54
 
        h_mapper = plot.value_mapper
55
 
 
56
 
    left = PlotAxis(orientation='left',
57
 
                    title= vtitle,
58
 
                    mapper=v_mapper,
59
 
                    component=plot)
60
 
 
61
 
    bottom = PlotAxis(orientation='bottom',
62
 
                      title= htitle,
63
 
                      mapper=h_mapper,
64
 
                      component=plot)
65
 
 
66
 
    plot.underlays.append(left)
67
 
    plot.underlays.append(bottom)
68
 
    return left, bottom
69
 
 
70
 
 
71
 
def add_default_grids(plot, orientation="normal", tick_gen=None):
72
 
    """
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.
76
 
    """
77
 
    if orientation in ("normal", "h"):
78
 
        v_mapper = plot.index_mapper
79
 
        h_mapper = plot.value_mapper
80
 
    else:
81
 
        v_mapper = plot.value_mapper
82
 
        h_mapper = plot.index_mapper
83
 
 
84
 
    vgrid = PlotGrid(mapper=v_mapper, orientation='vertical',
85
 
                     component=plot,
86
 
                     line_color="lightgray", line_style="dot",
87
 
                     tick_generator = tick_gen)
88
 
 
89
 
    hgrid = PlotGrid(mapper=h_mapper, orientation='horizontal',
90
 
                     component=plot,
91
 
                     line_color="lightgray", line_style="dot",
92
 
                     tick_generator = ScalesTickGenerator())
93
 
 
94
 
    plot.underlays.append(vgrid)
95
 
    plot.underlays.append(hgrid)
96
 
    return hgrid, vgrid
97
 
 
98
 
def _create_plot_component():
99
 
    container = OverlayPlotContainer(padding = 50, fill_padding = True,
100
 
                                     bgcolor = "lightgray", use_backbuffer=True)
101
 
 
102
 
    # Create the initial X-series of data
103
 
    numpoints = 100
104
 
    low = -5
105
 
    high = 15.0
106
 
    x = linspace(low, high, numpoints)
107
 
 
108
 
    now = time()
109
 
    timex = linspace(now, now+7*24*3600, numpoints)
110
 
 
111
 
    # Plot some bessel functions
112
 
    value_mapper = None
113
 
    index_mapper = None
114
 
    plots = {}
115
 
    for i in range(10):
116
 
        y = jn(i, x)
117
 
        if i%2 == 1:
118
 
            plot = create_line_plot((timex,y), color=tuple(COLOR_PALETTE[i]), width=2.0)
119
 
            plot.index.sort_order = "ascending"
120
 
        else:
121
 
            plot = create_scatter_plot((timex,y), color=tuple(COLOR_PALETTE[i]))
122
 
        plot.bgcolor = "white"
123
 
        plot.border_visible = True
124
 
        if i == 0:
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)
131
 
        else:
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)
136
 
 
137
 
        if i==0:
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)
145
 
 
146
 
        container.add(plot)
147
 
        plots["Bessel j_%d"%i] = plot
148
 
 
149
 
    # Set the list of plots on the legend
150
 
    legend.plots = plots
151
 
 
152
 
    # Add the title at the top
153
 
    container.overlays.append(PlotLabel("Bessel functions",
154
 
                              component=container,
155
 
                              font = "swiss 16",
156
 
                              overlay_position="top"))
157
 
 
158
 
    # Add the traits inspector tool to the container
159
 
    container.tools.append(TraitsTool(container))
160
 
 
161
 
    return container
162
 
 
163
 
#===============================================================================
164
 
# Attributes to use for the plot view.
165
 
size=(800,700)
166
 
title="Simple line plot"
167
 
 
168
 
#===============================================================================
169
 
# # Demo class that is used by the demo.py application.
170
 
#===============================================================================
171
 
class Demo(HasTraits):
172
 
    plot = Instance(Component)
173
 
 
174
 
    traits_view = View(
175
 
                    Group(
176
 
                        Item('plot', editor=ComponentEditor(size=size),
177
 
                             show_label=False),
178
 
                        orientation = "vertical"),
179
 
                    resizable=True, title=title,
180
 
                    width=size[0], height=size[1]
181
 
                    )
182
 
 
183
 
    def _plot_default(self):
184
 
         return _create_plot_component()
185
 
 
186
 
demo = Demo()
187
 
 
188
 
if __name__ == "__main__":
189
 
    demo.configure_traits()
190
 
 
191
 
#--EOF---