Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and clean up multiaxis.py example #513

Merged
merged 6 commits into from
Feb 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 58 additions & 45 deletions examples/demo/multiaxis.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env python
"""
Draws several overlapping line plots like simple_line.py, but uses a separate
Y range for each plot. Also has a second Y-axis on the right hand side.
Demonstrates use of the BroadcasterTool.

Left-drag pans the plot.

Right-click and dragging on the legend allows you to reposition the legend.

Double-clicking on line or scatter plots brings up a traits editor for the plot.
Y range for j1 compared to the other 3 curves. Also has a second Y-axis on the
right hand side for j1. Demonstrates use of the BroadcasterTool.

Interactive behavior:
* Left-drag pans the plot.
* Right-click and dragging on the legend allows you to reposition the legend.
* Double-clicking on line or scatter plots brings up a traits editor for the
plot.
"""

# Major library imports
Expand All @@ -19,22 +19,24 @@
# Enthought library imports
from enable.api import Component, ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import Item, Group, View
from traitsui.api import Item, VGroup, View

# Chaco imports
from chaco.api import create_line_plot, add_default_axes, \
add_default_grids, OverlayPlotContainer, \
PlotLabel, Legend, PlotAxis
from chaco.tools.api import (PanTool, LegendTool, LegendHighlighter,
TraitsTool, BroadcasterTool)
TraitsTool, BroadcasterTool, ZoomTool)

# =============================================================================
# Create the Chaco plot.
# =============================================================================


#===============================================================================
# # Create the Chaco plot.
#===============================================================================
def _create_plot_component():

container = OverlayPlotContainer(padding = 50, fill_padding = True,
bgcolor = "lightgray", use_backbuffer=True)
container = OverlayPlotContainer(padding=60, fill_padding=True,
use_backbuffer=True, border_visible=True)

# Create the initial X-series of data
numpoints = 100
Expand All @@ -47,78 +49,89 @@ def _create_plot_component():
broadcaster = BroadcasterTool()
for i in range(4):
y = jn(i, x)
plot = create_line_plot((x,y), color=tuple(COLOR_PALETTE[i]), width=2.0)
plot.index.sort_order = "ascending"
plot.bgcolor = "white"
plot.border_visible = True
plot = create_line_plot((x, y), color=tuple(COLOR_PALETTE[i]),
width=2.0)
if i == 0:
add_default_grids(plot)
add_default_axes(plot)

# Create a pan tool and give it a reference to the plot it should
# manipulate, but don't attach it to the plot. Instead, attach it to
# the broadcaster.
pan = PanTool(plot)
broadcaster.tools.append(pan)
left_axis, _ = add_default_axes(plot)
left_axis.title = "Bessel j0, j2, j3"
elif i != 1:
# Map correctly j2 and j3 on the first plot's axis:
plot0 = plots["Bessel j_0"]
plot.index_mapper = plot0.index_mapper
plot.value_mapper = plot0.value_mapper
plot0.value_mapper.range.add(plot.value)

# Create a pan/zoom tool and give it a reference to the plot it should
# manipulate, but don't attach it to the plot. Instead, attach it to
# the broadcaster. Do it only for each independent set of axis_mappers:
if i in [0, 1]:
pan = PanTool(component=plot)
broadcaster.tools.append(pan)

zoom = ZoomTool(component=plot)
broadcaster.tools.append(zoom)

container.add(plot)
plots["Bessel j_%d"%i] = plot
plots["Bessel j_%d" % i] = plot

# Add an axis on the right-hand side that corresponds to the second plot.
# Note that it uses plot.value_mapper instead of plot0.value_mapper.
plot1 = plots["Bessel j_1"]
axis = PlotAxis(plot1, orientation="right")
plot1.underlays.append(axis)
axis.title = "Bessel j1"

# Add the broadcast tool to the container, instead of to an
# individual plot
container.tools.append(broadcaster)
# Add the broadcast tool to one of the renderers: adding it to the
# container instead breaks the box mode of the ZoomTool:
plot0 = plots["Bessel j_0"]
plot0.tools.append(broadcaster)

# Create a legend, with tools to move it around and highlight renderers:
legend = Legend(component=container, padding=10, align="ur")
legend.tools.append(LegendTool(legend, drag_button="right"))
legend.tools.append(LegendHighlighter(legend))
container.overlays.append(legend)

# Set the list of plots on the legend
legend.plots = plots

# Add the title at the top
container.overlays.append(PlotLabel("Bessel functions",
component=container,
font = "swiss 16",
font="swiss 16",
overlay_position="top"))

# Add the traits inspector tool to the container
container.tools.append(TraitsTool(container))

return container

#===============================================================================

# =============================================================================
# Attributes to use for the plot view.
size=(800,700)
title="Multi-Y plot"
size = (800, 700)
title = "Multi-Y plot"

# =============================================================================
# Demo class that is used by the demo.py application.
# =============================================================================


#===============================================================================
# # Demo class that is used by the demo.py application.
#===============================================================================
class Demo(HasTraits):
plot = Instance(Component)

traits_view = View(
Group(
VGroup(
Item('plot', editor=ComponentEditor(size=size),
show_label=False),
orientation = "vertical"),
show_label=False)),
resizable=True, title=title,
width=size[0], height=size[1]
)
width=size[0], height=size[1])

def _plot_default(self):
return _create_plot_component()


demo = Demo()

if __name__ == "__main__":
demo.configure_traits()

#--EOF---