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

« back to all changes in this revision

Viewing changes to examples/demo/basic/scatter_toggle.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
 
Scatter plot with point selection
4
 
 
5
 
Draws a simple scatter plot of random data. The user can click on points to
6
 
select or unselect them.
7
 
 
8
 
    - Left-click on a point to select or unselect it.
9
 
    - Left-drag to pan.
10
 
    - Mouse wheel to zoom
11
 
    
12
 
"""
13
 
# FIXME: the 'z' zoom interaction is ill-behaved.
14
 
 
15
 
# Major library imports
16
 
from numpy import sort
17
 
from numpy.random import random
18
 
 
19
 
# Enthought library imports
20
 
from enable.api import Component, ComponentEditor
21
 
from traits.api import HasTraits, Instance
22
 
from traitsui.api import Item, VGroup, View, Label, HGroup, spring
23
 
 
24
 
# Chaco imports
25
 
from chaco.api import AbstractDataSource, ArrayPlotData, Plot, \
26
 
    ScatterInspectorOverlay
27
 
from chaco.tools.api import ScatterInspector, PanTool, ZoomTool
28
 
 
29
 
#===============================================================================
30
 
# # Create the Chaco plot.
31
 
#===============================================================================
32
 
def _create_plot_component():
33
 
 
34
 
    # Create some data
35
 
    npts = 100
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="slategray",
52
 
              marker_size=6,
53
 
              bgcolor="white")
54
 
 
55
 
    # Tweak some of the plot properties
56
 
    plot.title = "Scatter Plot With 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
 
    my_plot.tools.append(ScatterInspector(my_plot, selection_mode="toggle",
66
 
                                          persistent_hover=False))
67
 
    my_plot.overlays.append(
68
 
            ScatterInspectorOverlay(my_plot,
69
 
                hover_color = "transparent",
70
 
                hover_marker_size = 10,
71
 
                hover_outline_color = "purple",
72
 
                hover_line_width = 2,
73
 
                selection_marker_size = 8,
74
 
                selection_color = "lawngreen")
75
 
            )
76
 
 
77
 
    my_plot.tools.append(PanTool(my_plot))
78
 
    my_plot.overlays.append(ZoomTool(my_plot, drag_button="right"))
79
 
 
80
 
    return plot
81
 
 
82
 
#===============================================================================
83
 
# Attributes to use for the plot view.
84
 
size=(650,650)
85
 
title="Scatter plot with selection"
86
 
bg_color="lightgray"
87
 
 
88
 
#===============================================================================
89
 
# # Demo class that is used by the demo.py application.
90
 
#===============================================================================
91
 
class Demo(HasTraits):
92
 
    plot = Instance(Component)
93
 
 
94
 
    traits_view = View(
95
 
                    VGroup(
96
 
                        HGroup(spring, Label('Click point to select/unselect'), 
97
 
                            spring),
98
 
                        Item('plot', editor=ComponentEditor(size=size,
99
 
                                                            bgcolor=bg_color),
100
 
                             show_label=False),
101
 
                        orientation = "vertical"),
102
 
                    resizable=True, title=title
103
 
                    )
104
 
 
105
 
    def _metadata_handler(self):
106
 
        sel_indices = self.index_datasource.metadata.get('selections', [])
107
 
        print "Selection indices:", sel_indices
108
 
 
109
 
        hover_indices = self.index_datasource.metadata.get('hover', [])
110
 
        print "Hover indices:", hover_indices
111
 
 
112
 
    def _plot_default(self):
113
 
        plot = _create_plot_component()
114
 
 
115
 
        # Retrieve the plot hooked to the tool.
116
 
        my_plot = plot.plots["my_plot"][0]
117
 
 
118
 
        # Set up the trait handler for the selection
119
 
        self.index_datasource = my_plot.index
120
 
        self.index_datasource.on_trait_change(self._metadata_handler,
121
 
                                              "metadata_changed")
122
 
 
123
 
        return plot
124
 
 
125
 
demo = Demo()
126
 
 
127
 
if __name__ == "__main__":
128
 
    demo.configure_traits()
129
 
 
130
 
# EOF