2
Renders some contoured and colormapped images of a scalar value field.
3
- Left-drag pans the plot.
4
- Mousewheel up and down zooms the plot in and out.
5
- Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular
6
region to zoom. If you use a sequence of zoom boxes, pressing alt-left-arrow and alt-right-arrow moves you forwards and backwards through the "zoom
10
# Major library imports
11
from numpy import cos, linspace, log, meshgrid, pi, sin
13
# Enthought library imports
14
from enable.api import Component, ComponentEditor
15
from traits.api import HasTraits, Instance
16
from traitsui.api import Item, Group, View
19
from chaco.api import ArrayPlotData, ColorBar, gmt_drywet, \
20
HPlotContainer, LinearMapper, Plot
21
from chaco.tools.api import PanTool, ZoomTool
24
#===============================================================================
25
# # Create the Chaco plot.
26
#===============================================================================
27
def _create_plot_component():
29
# Create a scalar field to colormap
30
x_extents = (-2*pi, 2*pi)
31
y_extents = (-1.5*pi, 1.5*pi)
32
xs = linspace(-2*pi, 2*pi, 200)
33
ys = linspace(-1.5*pi, 1.5*pi, 100)
34
x, y = meshgrid(xs,ys)
35
zs = sin(log(abs((x+1)**4)+0.05))*cos(y)*1.1*(-y) + \
36
sin(((x+1)**2 + y**2)/4)
38
# Create a plot data obect and give it this data
40
pd.set_data("imagedata", zs)
42
# Create the left plot, a colormap and simple contours
44
lplot.img_plot("imagedata",
49
lplot.contour_plot("imagedata",
54
# Tweak some of the plot properties
55
lplot.title = "Colormap and contours"
57
lplot.bg_color = "white"
58
lplot.fill_padding = True
60
# Add some tools to the plot
61
zoom = ZoomTool(lplot, tool_mode="box", always_on=False)
62
lplot.overlays.append(zoom)
63
lplot.tools.append(PanTool(lplot, constrain_key="shift"))
65
# Right now, some of the tools are a little invasive, and we need the
66
# actual CMapImage object to give to them
67
cm_plot = lplot.plots["cm_plot"][0]
69
# Create the colorbar, handing in the appropriate range and colormap
70
colormap = cm_plot.color_mapper
71
colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range),
72
color_mapper=colormap,
78
colorbar.padding_top = lplot.padding_top
79
colorbar.padding_bottom = lplot.padding_bottom
81
# Create the left plot, contours of varying color and width
82
rplot = Plot(pd, range2d=lplot.range2d)
83
rplot.contour_plot("imagedata",
90
widths=list(linspace(4.0, 0.1, 15)),
93
# Add some tools to the plot
94
zoom = ZoomTool(rplot, tool_mode="box", always_on=False)
95
rplot.overlays.append(zoom)
96
rplot.tools.append(PanTool(rplot, constrain_key="shift"))
98
# Tweak some of the plot properties
99
rplot.title = "Varying contour lines"
101
rplot.bg_color = "white"
102
rplot.fill_padding = True
104
# Create a container and add our plots
105
container = HPlotContainer(padding=40, fill_padding=True,
106
bgcolor = "white", use_backbuffer=True)
107
container.add(colorbar)
112
#===============================================================================
113
# Attributes to use for the plot view.
115
title="Some contour plots"
118
#===============================================================================
119
# # Demo class that is used by the demo.py application.
120
#===============================================================================
121
class Demo(HasTraits):
122
plot = Instance(Component)
126
Item('plot', editor=ComponentEditor(size=size,
129
orientation = "vertical"),
130
resizable=True, title=title
132
def _plot_default(self):
133
return _create_plot_component()
137
if __name__ == "__main__":
138
demo.configure_traits()