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

« back to all changes in this revision

Viewing changes to examples/demo/functionplotter.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 use of the FunctionDataSource that depends on an external range
4
 
and returns different data depending on that range.
5
 
"""
6
 
 
7
 
# Major library imports
8
 
from numpy import linspace, sin, ceil
9
 
 
10
 
# Enthought library imports
11
 
from enable.api import Component, ComponentEditor
12
 
from traits.api import HasTraits, Instance, Int
13
 
from traitsui.api import Item, Group, HGroup, View
14
 
 
15
 
# Chaco imports
16
 
from chaco.api import ScatterPlot, DataView, LinePlot
17
 
from chaco.tools.api import PanTool, ZoomTool
18
 
from chaco.function_data_source import FunctionDataSource
19
 
 
20
 
 
21
 
class PlotExample(HasTraits):
22
 
    plot = Instance(Component)
23
 
    numpoints = Int(500)
24
 
 
25
 
    traits_view = View(
26
 
        Group(
27
 
            Item('plot', editor=ComponentEditor(), show_label=False),
28
 
            HGroup(
29
 
                HGroup(
30
 
                    Item('object.plot.x_mapper.range.low_setting', label='Low'),
31
 
                    Item('object.plot.x_mapper.range.high_setting', label='High'),
32
 
                    label='X', show_border=True
33
 
                ),
34
 
                HGroup(
35
 
                    Item('object.plot.y_mapper.range.low_setting', label='Low'),
36
 
                    Item('object.plot.y_mapper.range.high_setting', label='High'),
37
 
                    label='Y', show_border=True
38
 
                ),
39
 
            ),
40
 
            orientation = "vertical"), resizable=True, title="Function Plot",
41
 
            width=900, height=600
42
 
        )
43
 
 
44
 
    def xfunc(self, low, high):
45
 
        dx = (high - low) / self.numpoints
46
 
        real_low = ceil(low/dx) * dx
47
 
        real_high = ceil(high/dx) * dx
48
 
        return linspace(real_low, real_high, self.numpoints)
49
 
 
50
 
    def yfunc(self, low, high):
51
 
        x = self.xfunc(low, high)
52
 
        return sin(1.0/x)
53
 
 
54
 
    def _plot_default(self):
55
 
        container = DataView()
56
 
 
57
 
        xds = FunctionDataSource(func = self.xfunc)
58
 
        yds = FunctionDataSource(func = self.yfunc)
59
 
 
60
 
        xmapper = container.x_mapper
61
 
        ymapper = container.y_mapper
62
 
 
63
 
        xds.data_range = xmapper.range
64
 
        yds.data_range = xmapper.range
65
 
 
66
 
        xmapper.range.set_bounds(-5, 10)
67
 
        ymapper.range.set_bounds(-1, 1.2)
68
 
 
69
 
        plot = ScatterPlot(index = xds, value = yds, index_mapper = xmapper,
70
 
                           value_mapper = ymapper,
71
 
                           color = "green",
72
 
                           marker = "circle",
73
 
                           marker_size = 3,
74
 
                           line_width = 0)
75
 
 
76
 
        plot2 = LinePlot(index = xds, value = yds, index_mapper = xmapper,
77
 
                        value_mapper = ymapper,
78
 
                        color = "lightgray")
79
 
 
80
 
        container.add(plot2, plot)
81
 
        plot.tools.append(PanTool(plot, constrain_direction="x", constrain=True))
82
 
        plot.tools.append(ZoomTool(plot, axis="index", tool_mode="range"))
83
 
 
84
 
        return container
85
 
 
86
 
 
87
 
demo = PlotExample()
88
 
 
89
 
if __name__ == "__main__":
90
 
    demo.configure_traits()
91