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

« back to all changes in this revision

Viewing changes to examples/demo/basic/scatter_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
 
"""
2
 
Lasso selection of data points
3
 
 
4
 
Draws a simple scatterplot of random data.  Drag the mouse to use the lasso
5
 
selector, which allows you to circle all the points in a region.
6
 
 
7
 
Upon completion of the lasso operation, the indices of the selected points are
8
 
printed to the console.
9
 
 
10
 
Uncomment 'lasso_selection.incremental_select' line (line 74) to see the
11
 
indices of the selected points computed in real time.
12
 
"""
13
 
 
14
 
import sys
15
 
 
16
 
# Major library imports
17
 
from numpy import sort, compress, arange
18
 
from numpy.random import random
19
 
 
20
 
# Enthought library imports
21
 
from enable.api import Component, ComponentEditor
22
 
from traits.api import HasTraits, Instance
23
 
from traitsui.api import Item, Group, View
24
 
 
25
 
# Chaco imports
26
 
from chaco.api import ArrayPlotData, Plot, LassoOverlay
27
 
from chaco.tools.api import LassoSelection, ScatterInspector
28
 
 
29
 
#===============================================================================
30
 
# # Create the Chaco plot.
31
 
#===============================================================================
32
 
def _create_plot_component():
33
 
 
34
 
    # Create some data
35
 
    npts = 2000
36
 
    x = sort(random(npts))
37
 
    y = random(npts)
38
 
 
39
 
    # Create a plot data obect and give it this data
40
 
    pd = ArrayPlotData()
41
 
    pd.set_data("index", x)
42
 
    pd.set_data("value", y)
43
 
 
44
 
    # Create the plot
45
 
    plot = Plot(pd)
46
 
    plot.plot(("index", "value"),
47
 
              type="scatter",
48
 
              name="my_plot",
49
 
              marker="circle",
50
 
              index_sort="ascending",
51
 
              color="red",
52
 
              marker_size=4,
53
 
              bgcolor="white")
54
 
 
55
 
    # Tweak some of the plot properties
56
 
    plot.title = "Scatter Plot With Lasso Selection"
57
 
    plot.line_width = 1
58
 
    plot.padding = 50
59
 
 
60
 
    # Right now, some of the tools are a little invasive, and we need the
61
 
    # actual ScatterPlot object to give to them
62
 
    my_plot = plot.plots["my_plot"][0]
63
 
 
64
 
    # Attach some tools to the plot
65
 
    lasso_selection = LassoSelection(component=my_plot,
66
 
                                     selection_datasource=my_plot.index)
67
 
    my_plot.active_tool = lasso_selection
68
 
    my_plot.tools.append(ScatterInspector(my_plot))
69
 
    lasso_overlay = LassoOverlay(lasso_selection=lasso_selection,
70
 
                                 component=my_plot)
71
 
    my_plot.overlays.append(lasso_overlay)
72
 
 
73
 
    # Uncomment this if you would like to see incremental updates:
74
 
    #lasso_selection.incremental_select = True
75
 
 
76
 
    return plot
77
 
 
78
 
 
79
 
#===============================================================================
80
 
# Attributes to use for the plot view.
81
 
size=(650,650)
82
 
title="Scatter plot with selection"
83
 
bg_color="lightgray"
84
 
 
85
 
#===============================================================================
86
 
# # Demo class that is used by the demo.py application.
87
 
#===============================================================================
88
 
class Demo(HasTraits):
89
 
    plot = Instance(Component)
90
 
 
91
 
    traits_view = View(
92
 
                    Group(
93
 
                        Item('plot', editor=ComponentEditor(size=size),
94
 
                             show_label=False),
95
 
                        orientation = "vertical"),
96
 
                    resizable=True, title=title
97
 
                    )
98
 
 
99
 
    def _selection_changed(self):
100
 
        mask = self.index_datasource.metadata['selection']
101
 
        print "New selection: "
102
 
        print compress(mask, arange(len(mask)))
103
 
        # Ensure that the points are printed immediately:
104
 
        sys.stdout.flush()
105
 
        
106
 
 
107
 
    def _plot_default(self):
108
 
         plot = _create_plot_component()
109
 
 
110
 
         # Retrieve the plot hooked to the LassoSelection tool.
111
 
         my_plot = plot.plots["my_plot"][0]
112
 
         lasso_selection = my_plot.active_tool
113
 
 
114
 
         # Set up the trait handler for the selection
115
 
         self.index_datasource = my_plot.index
116
 
         lasso_selection.on_trait_change(self._selection_changed,
117
 
                                        'selection_changed')
118
 
 
119
 
         return plot
120
 
 
121
 
demo = Demo()
122
 
 
123
 
if __name__ == "__main__":
124
 
    demo.configure_traits()