3
Demonstrates use of the FunctionDataSource that depends on an external range
4
and returns different data depending on that range.
7
# Major library imports
8
from numpy import linspace, sin, ceil
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
16
from chaco.api import ScatterPlot, DataView, LinePlot
17
from chaco.tools.api import PanTool, ZoomTool
18
from chaco.function_data_source import FunctionDataSource
21
class PlotExample(HasTraits):
22
plot = Instance(Component)
27
Item('plot', editor=ComponentEditor(), show_label=False),
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
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
40
orientation = "vertical"), resizable=True, title="Function Plot",
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)
50
def yfunc(self, low, high):
51
x = self.xfunc(low, high)
54
def _plot_default(self):
55
container = DataView()
57
xds = FunctionDataSource(func = self.xfunc)
58
yds = FunctionDataSource(func = self.yfunc)
60
xmapper = container.x_mapper
61
ymapper = container.y_mapper
63
xds.data_range = xmapper.range
64
yds.data_range = xmapper.range
66
xmapper.range.set_bounds(-5, 10)
67
ymapper.range.set_bounds(-1, 1.2)
69
plot = ScatterPlot(index = xds, value = yds, index_mapper = xmapper,
70
value_mapper = ymapper,
76
plot2 = LinePlot(index = xds, value = yds, index_mapper = xmapper,
77
value_mapper = ymapper,
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"))
89
if __name__ == "__main__":
90
demo.configure_traits()