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

« back to all changes in this revision

Viewing changes to examples/demo/aspect_ratio.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
 
Controlling aspect ratio
3
 
 
4
 
Demonstrates various ways that Chaco can control aspect ratios in plots.
5
 
 
6
 
Left-drag pans the plot.
7
 
 
8
 
Mousewheel up and down zooms the plot in and out.
9
 
"""
10
 
 
11
 
# Major library imports
12
 
from itertools import chain
13
 
from numpy import sort, isfinite
14
 
from numpy.random import random
15
 
 
16
 
 
17
 
# Enthought library imports
18
 
from enable.api import ComponentEditor
19
 
from traits.api import Any, Bool, BaseFloat, HasTraits
20
 
from traitsui.api import Item, HGroup, VGroup, View
21
 
 
22
 
# Chaco imports
23
 
from chaco.api import ArrayPlotData, Plot
24
 
from chaco.tools.api import PanTool, ZoomTool
25
 
 
26
 
class AspectRatio(BaseFloat):
27
 
    "A new Trait for defining aspect ratios"
28
 
 
29
 
    default_value = 1.0
30
 
 
31
 
    info_text = "a nonzero floating point number"
32
 
 
33
 
    def validate(self, object, name, value):
34
 
        value = super(AspectRatio, self).validate(object, name, value)
35
 
        if value != 0.0 and isfinite(value):
36
 
            return value
37
 
        else:
38
 
            self.error(object, name, value)
39
 
 
40
 
 
41
 
class MyPlot(HasTraits):
42
 
 
43
 
    plot = Any()
44
 
    screen_enabled = Bool(False)
45
 
    screen_aspect = AspectRatio()
46
 
    fixed_x = Bool(False)
47
 
    fixed_y = Bool(False)
48
 
    traits_view = View(
49
 
                    VGroup(
50
 
                        HGroup(
51
 
                            Item("screen_enabled", label="Screen"),
52
 
                            Item("screen_aspect", label="aspect ratio (w/h)")
53
 
                            ),
54
 
                        HGroup(
55
 
                            Item("fixed_x", label="Data X fixed"),
56
 
                            Item("fixed_y", label="Data Y fixed")
57
 
                            ),
58
 
                        Item("plot", editor=ComponentEditor(size=(100,100)),
59
 
                             show_label=False)
60
 
                        ),
61
 
                    width=600, height=600, resizable=True,
62
 
                    title="Aspect Ratio Example")
63
 
 
64
 
 
65
 
    def __init__(self, *args, **kw):
66
 
        HasTraits.__init__(self, *args, **kw)
67
 
        numpoints = 200
68
 
        plotdata = ArrayPlotData(x=sort(random(numpoints)), y=random(numpoints))
69
 
        plot = Plot(plotdata)
70
 
        plot.plot(("x", "y"), type="scatter")
71
 
        plot.tools.append(PanTool(plot))
72
 
        plot.overlays.append(ZoomTool(plot))
73
 
        self.plot = plot
74
 
 
75
 
    def _screen_enabled_changed(self):
76
 
        if self.screen_enabled:
77
 
            self.plot.aspect_ratio = self.screen_aspect
78
 
        else:
79
 
            self.plot.aspect_ratio = None
80
 
        self.plot.request_redraw()
81
 
 
82
 
    def _screen_aspect_changed(self):
83
 
        if self.screen_enabled:
84
 
            self.plot.aspect_ratio = self.screen_aspect
85
 
            self.plot.request_redraw()
86
 
 
87
 
    def _fixed_x_changed(self):
88
 
        self.plot.x_mapper.stretch_data = not self.fixed_x
89
 
        # Also have to change all the renderers' mappers
90
 
        for renderer in chain(*self.plot.plots.values()):
91
 
            renderer.index_mapper.stretch_data = not self.fixed_x
92
 
        self.plot.request_redraw()
93
 
 
94
 
    def _fixed_y_changed(self):
95
 
        self.plot.y_mapper.stretch_data = not self.fixed_y
96
 
        for renderer in chain(*self.plot.plots.values()):
97
 
            renderer.value_mapper.stretch_data = not self.fixed_y
98
 
        self.plot.request_redraw()
99
 
 
100
 
#===============================================================================
101
 
# # Create the demo object to be used by the demo.py application.
102
 
#===============================================================================
103
 
demo = myplot = MyPlot()
104
 
 
105
 
if __name__ == "__main__":
106
 
    myplot.configure_traits()
 
 
b'\\ No newline at end of file'