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

« back to all changes in this revision

Viewing changes to examples/demo/basic/image_lasso.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
 
Demonstrates the LassoTool and overlay on a colormapped image
4
 
plot.  The underlying plot is similar to the one in cmap_image_plot.py.
5
 
 
6
 
Use Shift-drag to select multiple disjoint regions.
7
 
"""
8
 
 
9
 
# Major library imports
10
 
from numpy import linspace, meshgrid, pi, sin
11
 
 
12
 
# Enthought library imports
13
 
from enable.api import Component, ComponentEditor
14
 
from traits.api import HasTraits, Instance
15
 
from traitsui.api import Item, Group, View
16
 
 
17
 
# Chaco imports
18
 
from chaco.api import ArrayPlotData, jet, Plot, LassoOverlay
19
 
from chaco.tools.api import LassoSelection, LassoSelection
20
 
 
21
 
#===============================================================================
22
 
# # Create the Chaco plot.
23
 
#===============================================================================
24
 
 
25
 
def lasso_updated(lasso_tool, name, old, new_selections):
26
 
    # new_selections is a list of arrays of coordinates in dataspace.  It is a
27
 
    # list because the LassoSelection supports multiple, disjoint selection regions.
28
 
    for i, selection in enumerate(new_selections):
29
 
        print "Selection region", i
30
 
 
31
 
        # We first map to screen because the selection is stored as coordinates
32
 
        # in data space
33
 
        screen_pts = lasso_tool.plot.map_screen(selection)
34
 
 
35
 
        # Now map each point into the grid index
36
 
        for x, y in screen_pts:
37
 
            print "\t", lasso_tool.plot.map_index((x, y))
38
 
    return
39
 
 
40
 
def _create_plot_component():# Create a scalar field to colormap
41
 
    xbounds = (-2*pi, 2*pi, 600)
42
 
    ybounds = (-1.5*pi, 1.5*pi, 300)
43
 
    xs = linspace(*xbounds)
44
 
    ys = linspace(*ybounds)
45
 
    x, y = meshgrid(xs,ys)
46
 
    z = sin(x)*y
47
 
 
48
 
    # Create a plot data obect and give it this data
49
 
    pd = ArrayPlotData()
50
 
    pd.set_data("imagedata", z)
51
 
 
52
 
    # Create the plot
53
 
    plot = Plot(pd)
54
 
    img_plot = plot.img_plot("imagedata",
55
 
                             xbounds=xbounds[:2],
56
 
                             ybounds=ybounds[:2],
57
 
                             colormap=jet)[0]
58
 
 
59
 
    # Tweak some of the plot properties
60
 
    plot.title = "Image Plot with Lasso"
61
 
    plot.padding = 50
62
 
 
63
 
    lasso_selection = LassoSelection(component=img_plot)
64
 
    lasso_selection.on_trait_change(lasso_updated, "disjoint_selections")
65
 
    lasso_overlay = LassoOverlay(lasso_selection = lasso_selection, component=img_plot)
66
 
    img_plot.tools.append(lasso_selection)
67
 
    img_plot.overlays.append(lasso_overlay)
68
 
    return plot
69
 
 
70
 
#===============================================================================
71
 
# Attributes to use for the plot view.
72
 
size = (800, 600)
73
 
title="Image Plot with Lasso"
74
 
 
75
 
#===============================================================================
76
 
# # Demo class that is used by the demo.py application.
77
 
#===============================================================================
78
 
class Demo(HasTraits):
79
 
    plot = Instance(Component)
80
 
 
81
 
    traits_view = View(
82
 
                    Group(
83
 
                        Item('plot', editor=ComponentEditor(size=size),
84
 
                             show_label=False),
85
 
                        orientation = "vertical"),
86
 
                    resizable=True, title=title
87
 
                    )
88
 
 
89
 
    def _plot_default(self):
90
 
         return _create_plot_component()
91
 
 
92
 
demo = Demo()
93
 
 
94
 
if __name__ == "__main__":
95
 
    demo.configure_traits()
96
 
 
97
 
# EOF