3
Demonstrates how to limit the extent of grid lines
6
# Major library imports
7
from numpy import array, linspace, zeros
8
from scipy.special import jn
10
# Enthought library imports
11
from enable.api import Component, ComponentEditor
12
from traits.api import HasTraits, Instance
13
from traitsui.api import Item, Group, View
16
from chaco.api import ArrayPlotData, HPlotContainer, Plot
17
from chaco.tools.api import PanTool, ZoomTool
19
#===============================================================================
20
# # Create the Chaco plot.
21
#===============================================================================
22
def _create_plot_component():
24
# Create some x-y data series to plot
25
x = linspace(-2.0, 10.0, 100)
26
pd = ArrayPlotData(index = x)
28
pd.set_data("y" + str(i), jn(i,x))
30
# Create some line plots of some of the data
31
plot = Plot(pd, title="Line Plot", padding=50, border_visible=True)
32
plot.legend.visible = True
33
plot.plot(("index", "y0", "y1", "y2"), name="j_n, n<3", color="auto")
34
plot.plot(("index", "y3"), name="j_3", color="auto")
36
plot.x_grid.line_color = "black"
37
plot.y_grid.line_color = "black"
39
ymin, ymax = 0.2, 0.80001
40
plot.x_grid.set(data_min = xmin, data_max = xmax,
41
transverse_bounds = (ymin, ymax),
42
transverse_mapper = plot.y_mapper)
44
plot.y_grid.set(data_min = ymin, data_max = ymax,
45
transverse_bounds = (xmin, xmax),
46
transverse_mapper = plot.x_mapper)
48
# Attach some tools to the plot
49
plot.tools.append(PanTool(plot))
50
zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
51
plot.overlays.append(zoom)
53
# A second plot whose vertical grid lines are clipped to the jn(3) function
54
def my_bounds_func(ticks):
55
""" Returns y_low and y_high for each grid tick in the array **ticks** """
56
tmp = array([zeros(len(ticks)),jn(3, ticks)]).T
59
func_plot = Plot(pd, padding=50, border_visible=True)
60
func_plot.plot(("index", "y3"), color="red")
61
func_plot.x_grid.set(transverse_bounds = my_bounds_func,
62
transverse_mapper = func_plot.y_mapper,
64
func_plot.tools.append(PanTool(func_plot))
66
container = HPlotContainer()
68
container.add(func_plot)
72
#===============================================================================
73
# Attributes to use for the plot view.
75
title="Grids with bounded extents"
77
#===============================================================================
78
# # Demo class that is used by the demo.py application.
79
#===============================================================================
80
class Demo(HasTraits):
81
plot = Instance(Component)
85
Item('plot', editor=ComponentEditor(size=size),
87
orientation = "vertical"),
88
resizable=True, title=title
91
def _plot_default(self):
92
return _create_plot_component()
96
if __name__ == "__main__":
97
demo.configure_traits()