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

« back to all changes in this revision

Viewing changes to examples/demo/basic/polygon_plot_demo.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
 
"""Demonstrate a simple polygon plot.  The UI allows you to change
2
 
some of the attributes of the plot.
3
 
"""
4
 
 
5
 
import numpy as np
6
 
 
7
 
from traits.api import HasTraits, Instance, Range
8
 
from traitsui.api import View, UItem, Item, Group, HGroup, VGroup, spring
9
 
from chaco.api import Plot, ArrayPlotData, PolygonPlot
10
 
from enable.api import ComponentEditor, LineStyle
11
 
 
12
 
 
13
 
class PolygonPlotDemo(HasTraits):
14
 
 
15
 
    # The main plot container.
16
 
    plot = Instance(Plot)
17
 
 
18
 
    # Data holder for `plot`.
19
 
    apd = Instance(ArrayPlotData)
20
 
 
21
 
    # The polygon plot renderer.
22
 
    polygon_plot = Instance(PolygonPlot)
23
 
 
24
 
    # Assorted styles that will be set on `polygon_plot`.    
25
 
    edge_style = LineStyle
26
 
    edge_width = Range(value=1, low=0, high=8)
27
 
    edge_alpha = Range(value=1.0, low=0.0, high=1.0)
28
 
    face_alpha = Range(value=0.4, low=0.0, high=1.0)
29
 
    alpha = Range(value=1.0, low=0.0, high=1.0)
30
 
 
31
 
    traits_view = \
32
 
        View(
33
 
            VGroup(
34
 
                Group(
35
 
                    UItem('plot', editor=ComponentEditor(), style='custom'),
36
 
                ),
37
 
                VGroup(
38
 
                    HGroup(
39
 
                        Item('edge_style'),
40
 
                        spring,
41
 
                    ),
42
 
                    Item('edge_width'),
43
 
                    Item('edge_alpha'),
44
 
                    Item('face_alpha'),
45
 
                    Item('alpha'),
46
 
                ),
47
 
            ),
48
 
            resizable=True,
49
 
        )
50
 
 
51
 
    #----------------------------------------------------------------------
52
 
    # Default values
53
 
    #----------------------------------------------------------------------
54
 
 
55
 
    def _apd_default(self):
56
 
        # Create the data to plot.
57
 
        px = np.array([0.5, 1.0, 2.0, 2.5, 2.0, 1.5, 0.5, 0.0])
58
 
        py = np.array([0.0, 0.8, 0.5, 3.0, 3.5, 2.0, 3.0, 0.5])
59
 
 
60
 
        # Create the ArrayPlotData container used by the Plot.
61
 
        apd = ArrayPlotData(px=px, py=py)
62
 
        return apd
63
 
 
64
 
    def _plot_default(self):
65
 
        plot = Plot(self.apd, title='PolygonPlot Demo')
66
 
        return plot
67
 
 
68
 
    def _polygon_plot_default(self):
69
 
        p = self.plot.plot(('px', 'py'),
70
 
                  type='polygon',
71
 
                  face_color=(0,0.8,1) + (self.face_alpha,),
72
 
                  edge_color=(0,0,0) + (self.edge_alpha,),
73
 
                  edge_style=self.edge_style,
74
 
                  alpha=self.alpha)
75
 
        return p[0]
76
 
 
77
 
    #----------------------------------------------------------------------
78
 
    # Trait change handlers
79
 
    #----------------------------------------------------------------------
80
 
 
81
 
    def _edge_style_changed(self):
82
 
        self.polygon_plot.edge_style = self.edge_style
83
 
 
84
 
    def _edge_width_changed(self):
85
 
        self.polygon_plot.edge_width = self.edge_width
86
 
 
87
 
    def _edge_alpha_changed(self):
88
 
        self.polygon_plot.edge_color = self.polygon_plot.edge_color[:3] + (self.edge_alpha,)
89
 
 
90
 
    def _face_alpha_changed(self):
91
 
        self.polygon_plot.face_color = self.polygon_plot.face_color[:3] + (self.face_alpha,)
92
 
 
93
 
    def _alpha_changed(self):
94
 
        self.polygon_plot.alpha = self.alpha
95
 
 
96
 
 
97
 
demo = PolygonPlotDemo()
98
 
# Hack to force initial rendering of the plot.
99
 
demo.face_alpha = 0.5
100
 
 
101
 
 
102
 
if __name__ == "__main__":
103
 
    demo.configure_traits()