~ubuntu-branches/ubuntu/precise/python-chaco/precise

« back to all changes in this revision

Viewing changes to examples/scales_test.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2008-12-29 02:34:05 UTC
  • Revision ID: james.westby@ubuntu.com-20081229023405-x7i4kp9mdxzmdnvu
Tags: upstream-3.0.1
ImportĀ upstreamĀ versionĀ 3.0.1

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 enthought.chaco.example_support import COLOR_PALETTE
 
24
from enthought.enable.example_support import DemoFrame, demo_main
 
25
 
 
26
# Enthought library imports
 
27
from enthought.enable.api import Window
 
28
 
 
29
# Chaco imports
 
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
 
34
 
 
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
 
38
 
 
39
def add_default_axes(plot, orientation="normal", vtitle="",htitle=""):
 
40
    """
 
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.
 
44
    """
 
45
    if orientation in ("normal", "h"):
 
46
        v_mapper = plot.value_mapper
 
47
        h_mapper = plot.index_mapper
 
48
    else:
 
49
        v_mapper = plot.index_mapper
 
50
        h_mapper = plot.value_mapper
 
51
 
 
52
    left = PlotAxis(orientation='left',
 
53
                    title= vtitle,
 
54
                    mapper=v_mapper,
 
55
                    component=plot)
 
56
 
 
57
    bottom = PlotAxis(orientation='bottom',
 
58
                      title= htitle,
 
59
                      mapper=h_mapper,
 
60
                      component=plot)
 
61
 
 
62
    plot.underlays.append(left)
 
63
    plot.underlays.append(bottom)
 
64
    return left, bottom
 
65
 
 
66
 
 
67
def add_default_grids(plot, orientation="normal", tick_gen=None):
 
68
    """
 
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.
 
72
    """
 
73
    if orientation in ("normal", "h"):
 
74
        v_mapper = plot.index_mapper
 
75
        h_mapper = plot.value_mapper
 
76
    else:
 
77
        v_mapper = plot.value_mapper
 
78
        h_mapper = plot.index_mapper
 
79
 
 
80
    vgrid = PlotGrid(mapper=v_mapper, orientation='vertical',
 
81
                     component=plot,
 
82
                     line_color="lightgray", line_style="dot",
 
83
                     tick_generator = tick_gen)
 
84
 
 
85
    hgrid = PlotGrid(mapper=h_mapper, orientation='horizontal',
 
86
                     component=plot,
 
87
                     line_color="lightgray", line_style="dot",
 
88
                     tick_generator = ScalesTickGenerator())
 
89
 
 
90
    plot.underlays.append(vgrid)
 
91
    plot.underlays.append(hgrid)
 
92
    return hgrid, vgrid
 
93
 
 
94
 
 
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
 
100
 
 
101
        # Create the initial X-series of data
 
102
        numpoints = 100
 
103
        low = -5
 
104
        high = 15.0
 
105
        x = linspace(low, high, numpoints)
 
106
 
 
107
        now = time()
 
108
        timex = linspace(now, now+7*24*3600, numpoints)
 
109
 
 
110
        # Plot some bessel functions
 
111
        value_mapper = None
 
112
        index_mapper = None
 
113
        plots = {}
 
114
        for i in range(10):
 
115
            y = jn(i, x)
 
116
            if i%2 == 1:
 
117
                plot = create_line_plot((timex,y), color=tuple(COLOR_PALETTE[i]), width=2.0)
 
118
                plot.index.sort_order = "ascending"
 
119
            else:
 
120
                plot = create_scatter_plot((timex,y), color=tuple(COLOR_PALETTE[i]))
 
121
            plot.bgcolor = "white"
 
122
            plot.border_visible = True
 
123
            if i == 0:
 
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)
 
130
            else:
 
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)
 
135
 
 
136
            if i==0:
 
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)
 
144
 
 
145
            container.add(plot)
 
146
            plots["Bessel j_%d"%i] = plot
 
147
 
 
148
        # Set the list of plots on the legend
 
149
        legend.plots = plots
 
150
 
 
151
        # Add the title at the top
 
152
        container.overlays.append(PlotLabel("Bessel functions",
 
153
                                  component=container,
 
154
                                  font = "swiss 16",
 
155
                                  overlay_position="top"))
 
156
 
 
157
        # Add the traits inspector tool to the container
 
158
        container.tools.append(TraitsTool(container))
 
159
 
 
160
        return Window(self, -1, component=container)
 
161
 
 
162
if __name__ == "__main__":
 
163
    demo_main(PlotFrame, size=(800,700), title="Simple line plot")
 
164
 
 
165
# EOF