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

« back to all changes in this revision

Viewing changes to examples/demo/basic/line_drawing.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
 
Line drawing tool
3
 
 
4
 
Demonstrates using a line segment drawing tool on top of the
5
 
scatter plot from simple_scatter.py.
6
 
 
7
 
Pan using right-drag.
8
 
Zoom interactions are the same as in simple_line.py.
9
 
 
10
 
Line segment drawing:
11
 
    - left click places a new point
12
 
    - moving over an existing point and left-dragging will reposition that point
13
 
    - moving over an existing point and ctrl-left-clicking will delete that point
14
 
    - pressing "Enter" will "finalize" the selection.  This means that the
15
 
      tool's _finalize_selection() method will be called, and the list of
16
 
      drawn points will be reset.  By default, _finalize_selection() does nothing,
17
 
      but subclasses can customize this.
18
 
"""
19
 
 
20
 
# Major library imports
21
 
from numpy import sort
22
 
from numpy.random import random
23
 
 
24
 
# Enthought library imports
25
 
from enable.api import Component, ComponentEditor
26
 
from traits.api import HasTraits, Instance
27
 
from traitsui.api import Item, Group, View
28
 
 
29
 
# Chaco imports
30
 
from chaco.api import ArrayPlotData, Plot
31
 
from chaco.tools.api import LineSegmentTool, PanTool, ZoomTool
32
 
 
33
 
 
34
 
class MyLineDrawer(LineSegmentTool):
35
 
    """
36
 
    This class demonstrates how to customize the behavior of the
37
 
    LineSegmentTool via subclassing.
38
 
    """
39
 
 
40
 
    def _finalize_selection(self):
41
 
        print "Dataspace points:"
42
 
        for point in self.points:
43
 
            print "\t", point
44
 
 
45
 
#===============================================================================
46
 
# # Create the Chaco plot.
47
 
#===============================================================================
48
 
def _create_plot_component():
49
 
 
50
 
    # Create some data
51
 
    numpts = 1000
52
 
    x = sort(random(numpts))
53
 
    y = random(numpts)
54
 
 
55
 
    # Create a plot data obect and give it this data
56
 
    pd = ArrayPlotData()
57
 
    pd.set_data("index", x)
58
 
    pd.set_data("value", y)
59
 
 
60
 
    # Create the plot
61
 
    plot = Plot(pd)
62
 
    plot.plot(("index", "value"),
63
 
              type="scatter",
64
 
              name="my_plot",
65
 
              marker="square",
66
 
              index_sort="ascending",
67
 
              color="lightblue",
68
 
              outline_color="none",
69
 
              marker_size=3,
70
 
              bgcolor="white")
71
 
 
72
 
    # Tweak some of the plot properties
73
 
    plot.title = "Click to add points, press Enter to finalize selection"
74
 
    plot.padding = 50
75
 
    plot.line_width = 1
76
 
 
77
 
    # Attach some tools to the plot
78
 
    pan = PanTool(plot, drag_button="right", constrain_key="shift")
79
 
    plot.tools.append(pan)
80
 
    zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
81
 
    plot.overlays.append(zoom)
82
 
    plot.overlays.append(MyLineDrawer(plot))
83
 
    return plot
84
 
 
85
 
#===============================================================================
86
 
# Attributes to use for the plot view.
87
 
size=(650,650)
88
 
title="Line drawing example"
89
 
bg_color="lightgray"
90
 
 
91
 
#===============================================================================
92
 
# # Demo class that is used by the demo.py application.
93
 
#===============================================================================
94
 
class Demo(HasTraits):
95
 
    plot = Instance(Component)
96
 
 
97
 
    traits_view = View(
98
 
                    Group(
99
 
                        Item('plot', editor=ComponentEditor(size=size,
100
 
                                                            bgcolor=bg_color),
101
 
                             show_label=False),
102
 
                        orientation = "vertical"),
103
 
                    resizable=True, title=title
104
 
                    )
105
 
 
106
 
    def _plot_default(self):
107
 
         return _create_plot_component()
108
 
 
109
 
demo = Demo()
110
 
 
111
 
if __name__ == "__main__":
112
 
    demo.configure_traits()