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

« back to all changes in this revision

Viewing changes to examples/demo/basic/cmap_image_select.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
 
#!/usr/bin/env python
2
 
"""
3
 
Draws a colormapped image plot
4
 
 - Left-drag pans the plot.
5
 
 - Mousewheel up and down zooms the plot in and out.
6
 
 - Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular
7
 
   region to zoom.  If you use a sequence of zoom boxes, pressing alt-left-arrow
8
 
   and alt-right-arrow moves you forwards and backwards through the "zoom
9
 
   history".
10
 
"""
11
 
 
12
 
# Major library imports
13
 
from numpy import linspace, meshgrid, pi
14
 
from scipy.special import jn
15
 
 
16
 
# Enthought library imports
17
 
from enable.api import Component, ComponentEditor
18
 
from traits.api import HasTraits, Instance
19
 
from traitsui.api import Item, Group, View
20
 
 
21
 
# Chaco imports
22
 
from chaco.api import ArrayPlotData, ColorBar, HPlotContainer, jet, \
23
 
                                 LinearMapper, Plot
24
 
from chaco.tools.api import PanTool, RangeSelection, \
25
 
                                       RangeSelectionOverlay, ZoomTool
26
 
 
27
 
#===============================================================================
28
 
# # Create the Chaco plot.
29
 
#===============================================================================
30
 
def _create_plot_component():
31
 
    # Create a scalar field to colormap# Create a scalar field to colormap
32
 
    xbounds = (-2*pi, 2*pi, 600)
33
 
    ybounds = (-1.5*pi, 1.5*pi, 300)
34
 
    xs = linspace(*xbounds)
35
 
    ys = linspace(*ybounds)
36
 
    x, y = meshgrid(xs,ys)
37
 
    z = jn(2, x)*y*x
38
 
 
39
 
    # Create a plot data obect and give it this data
40
 
    pd = ArrayPlotData()
41
 
    pd.set_data("imagedata", z)
42
 
 
43
 
    # Create the plot
44
 
    plot = Plot(pd)
45
 
    plot.img_plot("imagedata",
46
 
                  name="my_plot",
47
 
                  xbounds=xbounds[:2],
48
 
                  ybounds=ybounds[:2],
49
 
                  colormap=jet)
50
 
 
51
 
    # Tweak some of the plot properties
52
 
    plot.title = "Selectable Image Plot"
53
 
    plot.padding = 50
54
 
 
55
 
    # Right now, some of the tools are a little invasive, and we need the
56
 
    # actual CMapImage object to give to them
57
 
    my_plot = plot.plots["my_plot"][0]
58
 
 
59
 
    # Attach some tools to the plot
60
 
    plot.tools.append(PanTool(plot))
61
 
    zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
62
 
    plot.overlays.append(zoom)
63
 
 
64
 
    # Create the colorbar, handing in the appropriate range and colormap
65
 
    colormap = my_plot.color_mapper
66
 
    colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range),
67
 
                        color_mapper=colormap,
68
 
                        plot=my_plot,
69
 
                        orientation='v',
70
 
                        resizable='v',
71
 
                        width=30,
72
 
                        padding=20)
73
 
    colorbar.padding_top = plot.padding_top
74
 
    colorbar.padding_bottom = plot.padding_bottom
75
 
 
76
 
    # create a range selection for the colorbar
77
 
    range_selection = RangeSelection(component=colorbar)
78
 
    colorbar.tools.append(range_selection)
79
 
    colorbar.overlays.append(RangeSelectionOverlay(component=colorbar,
80
 
                                                   border_color="white",
81
 
                                                   alpha=0.8,
82
 
                                                   fill_color="lightgray"))
83
 
 
84
 
    # we also want to the range selection to inform the cmap plot of
85
 
    # the selection, so set that up as well
86
 
    range_selection.listeners.append(my_plot)
87
 
 
88
 
    # Create a container to position the plot and the colorbar side-by-side
89
 
    container = HPlotContainer(use_backbuffer = True)
90
 
    container.add(plot)
91
 
    container.add(colorbar)
92
 
    container.bgcolor = "lightgray"
93
 
 
94
 
    #my_plot.set_value_selection((-1.3, 6.9))
95
 
 
96
 
    return container
97
 
#===============================================================================
98
 
# Attributes to use for the plot view.
99
 
size=(800,600)
100
 
title="Colormapped Image Plot"
101
 
 
102
 
#===============================================================================
103
 
# # Demo class that is used by the demo.py application.
104
 
#===============================================================================
105
 
class Demo(HasTraits):
106
 
    plot = Instance(Component)
107
 
 
108
 
    traits_view = View(
109
 
                    Group(
110
 
                        Item('plot', editor=ComponentEditor(size=size),
111
 
                             show_label=False),
112
 
                        orientation = "vertical"),
113
 
                    resizable=True, title=title
114
 
                    )
115
 
    def _plot_default(self):
116
 
         return _create_plot_component()
117
 
 
118
 
demo = Demo()
119
 
 
120
 
if __name__ == "__main__":
121
 
    demo.configure_traits()
122