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

« back to all changes in this revision

Viewing changes to examples/demo/basic/contour_cmap_plot.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
 
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
7
 
   history".
8
 
"""
9
 
 
10
 
# Major library imports
11
 
from numpy import cos, linspace, log, meshgrid, pi, sin
12
 
 
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
17
 
 
18
 
# Chaco imports
19
 
from chaco.api import ArrayPlotData, ColorBar, gmt_drywet, \
20
 
                                 HPlotContainer, LinearMapper, Plot
21
 
from chaco.tools.api import PanTool, ZoomTool
22
 
 
23
 
 
24
 
#===============================================================================
25
 
# # Create the Chaco plot.
26
 
#===============================================================================
27
 
def _create_plot_component():
28
 
 
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)
37
 
 
38
 
    # Create a plot data obect and give it this data
39
 
    pd = ArrayPlotData()
40
 
    pd.set_data("imagedata", zs)
41
 
 
42
 
    # Create the left plot, a colormap and simple contours
43
 
    lplot = Plot(pd)
44
 
    lplot.img_plot("imagedata",
45
 
                   name="cm_plot",
46
 
                   xbounds=x_extents,
47
 
                   ybounds=y_extents,
48
 
                   colormap=gmt_drywet)
49
 
    lplot.contour_plot("imagedata",
50
 
                       type="line",
51
 
                       xbounds=x_extents,
52
 
                       ybounds=y_extents)
53
 
 
54
 
    # Tweak some of the plot properties
55
 
    lplot.title = "Colormap and contours"
56
 
    lplot.padding = 20
57
 
    lplot.bg_color = "white"
58
 
    lplot.fill_padding = True
59
 
 
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"))
64
 
 
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]
68
 
 
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,
73
 
                        plot=cm_plot,
74
 
                        orientation='v',
75
 
                        resizable='v',
76
 
                        width=30,
77
 
                        padding=20)
78
 
    colorbar.padding_top = lplot.padding_top
79
 
    colorbar.padding_bottom = lplot.padding_bottom
80
 
 
81
 
    # Create the left plot, contours of varying color and width
82
 
    rplot = Plot(pd, range2d=lplot.range2d)
83
 
    rplot.contour_plot("imagedata",
84
 
                       type="line",
85
 
                       xbounds=x_extents,
86
 
                       ybounds=y_extents,
87
 
                       bgcolor="black",
88
 
                       levels=15,
89
 
                       styles="solid",
90
 
                       widths=list(linspace(4.0, 0.1, 15)),
91
 
                       colors=gmt_drywet)
92
 
 
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"))
97
 
 
98
 
    # Tweak some of the plot properties
99
 
    rplot.title = "Varying contour lines"
100
 
    rplot.padding = 20
101
 
    rplot.bg_color = "white"
102
 
    rplot.fill_padding = True
103
 
 
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)
108
 
    container.add(lplot)
109
 
    container.add(rplot)
110
 
    return container
111
 
 
112
 
#===============================================================================
113
 
# Attributes to use for the plot view.
114
 
size=(950,650)
115
 
title="Some contour plots"
116
 
bg_color="lightgray"
117
 
 
118
 
#===============================================================================
119
 
# # Demo class that is used by the demo.py application.
120
 
#===============================================================================
121
 
class Demo(HasTraits):
122
 
    plot = Instance(Component)
123
 
 
124
 
    traits_view = View(
125
 
                    Group(
126
 
                        Item('plot', editor=ComponentEditor(size=size,
127
 
                                                            bgcolor=bg_color),
128
 
                             show_label=False),
129
 
                        orientation = "vertical"),
130
 
                    resizable=True, title=title
131
 
                    )
132
 
    def _plot_default(self):
133
 
         return _create_plot_component()
134
 
 
135
 
demo = Demo()
136
 
 
137
 
if __name__ == "__main__":
138
 
    demo.configure_traits()
139
 
 
140
 
# EOF