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

« back to all changes in this revision

Viewing changes to examples/demo/quiver.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
 
Draws a vector or "quiver" plot of a set of random points.
3
 
 - Left-drag pans the plot.
4
 
 - Mousewheel up and down zooms the plot in and out.
5
 
 - Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular
6
 
   region to zoom.  If you use a sequence of zoom boxes, pressing alt-left-arrow
7
 
   and alt-right-arrow moves you forwards and backwards through the "zoom
8
 
   history".
9
 
"""
10
 
 
11
 
# Major library imports
12
 
from numpy import array, sort
13
 
from numpy.random import random
14
 
 
15
 
# Enthought library imports
16
 
from enable.api import Component, ComponentEditor
17
 
from traits.api import HasTraits, Instance, Int
18
 
from traitsui.api import Item, View
19
 
 
20
 
# Chaco imports
21
 
from chaco.api import ArrayDataSource, MultiArrayDataSource, DataRange1D, \
22
 
        LinearMapper, QuiverPlot, OverlayPlotContainer, add_default_grids, \
23
 
        add_default_axes
24
 
from chaco.tools.api import PanTool, ZoomTool
25
 
 
26
 
 
27
 
class PlotExample(HasTraits):
28
 
    plot = Instance(Component)
29
 
    numpts = Int(400)
30
 
    vectorlen = Int(15)
31
 
 
32
 
    traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False),
33
 
                       width=600, height=600)
34
 
 
35
 
    def _plot_default(self):
36
 
        # Create starting points for the vectors.
37
 
        numpts = self.numpts
38
 
        x = sort(random(numpts))
39
 
        y = random(numpts)
40
 
 
41
 
        # Create vectors.
42
 
        vectorlen = self.vectorlen
43
 
        vectors = array((random(numpts)*vectorlen, random(numpts)*vectorlen)).T
44
 
 
45
 
        # Create an array data sources to plot all vectors at once
46
 
        xs = ArrayDataSource(x, sort_order='ascending')
47
 
        ys = ArrayDataSource(y)
48
 
        vector_ds = MultiArrayDataSource(vectors)
49
 
 
50
 
        # Set up the Plot
51
 
        xrange = DataRange1D()
52
 
        xrange.add(xs)
53
 
        yrange = DataRange1D()
54
 
        yrange.add(ys)
55
 
 
56
 
        quiverplot = QuiverPlot(index = xs, value = ys,
57
 
                        vectors = vector_ds,
58
 
                        index_mapper = LinearMapper(range=xrange),
59
 
                        value_mapper = LinearMapper(range=yrange),
60
 
                        bgcolor = "white")
61
 
 
62
 
        add_default_axes(quiverplot)
63
 
        add_default_grids(quiverplot)
64
 
 
65
 
        # Attach some tools to the plot
66
 
        quiverplot.tools.append(PanTool(quiverplot, constrain_key="shift"))
67
 
        zoom = ZoomTool(quiverplot)
68
 
        quiverplot.overlays.append(zoom)
69
 
 
70
 
        container = OverlayPlotContainer(quiverplot, padding=50)
71
 
 
72
 
        return container
73
 
 
74
 
 
75
 
demo = PlotExample()
76
 
 
77
 
if __name__ == "__main__":
78
 
    demo.configure_traits()