1
"""Demonstrate a simple polygon plot. The UI allows you to change
2
some of the attributes of the plot.
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
13
class PolygonPlotDemo(HasTraits):
15
# The main plot container.
18
# Data holder for `plot`.
19
apd = Instance(ArrayPlotData)
21
# The polygon plot renderer.
22
polygon_plot = Instance(PolygonPlot)
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)
35
UItem('plot', editor=ComponentEditor(), style='custom'),
51
#----------------------------------------------------------------------
53
#----------------------------------------------------------------------
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])
60
# Create the ArrayPlotData container used by the Plot.
61
apd = ArrayPlotData(px=px, py=py)
64
def _plot_default(self):
65
plot = Plot(self.apd, title='PolygonPlot Demo')
68
def _polygon_plot_default(self):
69
p = self.plot.plot(('px', 'py'),
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,
77
#----------------------------------------------------------------------
78
# Trait change handlers
79
#----------------------------------------------------------------------
81
def _edge_style_changed(self):
82
self.polygon_plot.edge_style = self.edge_style
84
def _edge_width_changed(self):
85
self.polygon_plot.edge_width = self.edge_width
87
def _edge_alpha_changed(self):
88
self.polygon_plot.edge_color = self.polygon_plot.edge_color[:3] + (self.edge_alpha,)
90
def _face_alpha_changed(self):
91
self.polygon_plot.face_color = self.polygon_plot.face_color[:3] + (self.face_alpha,)
93
def _alpha_changed(self):
94
self.polygon_plot.alpha = self.alpha
97
demo = PolygonPlotDemo()
98
# Hack to force initial rendering of the plot.
102
if __name__ == "__main__":
103
demo.configure_traits()