Chaco - Getting multiple data series to use the same axes and maps

*爱你&永不变心* 提交于 2019-12-08 13:48:17

问题


I am trying to plot several collections of data on a single plot.

Each dataset can be represented as an x-series (index) and several y-series (values). The ranges of x and y data series may be different in each data set. I want to have several of these data sets display on one plot. However, when I simply add the second plot object to the first (see below) it makes a second axis for it that is nested inside the plot.

I want both plots to share the same axis and for the axis bounds to be updated to fit all the data. What is the best way to achieve this? I am struggling to find topics on this in the documentation.

Thanks for your help. The code below highlights my problem.

# Major library imports
from numpy import linspace
from scipy.special import jn

from chaco.example_support import COLOR_PALETTE
# Enthought library imports
from enable.api import Component, ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import Item, Group, View

# Chaco imports
from chaco.api import ArrayPlotData, Plot
from chaco.tools.api import BroadcasterTool, PanTool, ZoomTool
from chaco.api import create_line_plot, add_default_axes

def _create_plot_component():    
    # Create some x-y data series to plot
    x = linspace(-2.0, 10.0, 100)
    x2 =linspace(-5.0, 10.0, 100)

    pd = ArrayPlotData(index = x)
    for i in range(5):
        pd.set_data("y" + str(i), jn(i,x))

    #slightly different plot data
    pd2 =  ArrayPlotData(index = x2)
    for i in range(5):
        pd2.set_data("y" + str(i), 2*jn(i,x2))

    # Create some line plots of some of the data
    plot1 = Plot(pd)
    plot1.plot(("index", "y0", "y1", "y2"), name="j_n, n<3", color="red")

    # Tweak some of the plot properties
    plot1.title = "My First Line Plot"
    plot1.padding = 50
    plot1.padding_top = 75
    plot1.legend.visible = True

    plot2 = Plot(pd2)
    plot2.plot(("index", "y0", "y1"), name="j_n, n<3", color="green")

    plot1.add(plot2)
    # Attach some tools to the plot
    broadcaster = BroadcasterTool()
    broadcaster.tools.append(PanTool(plot1))
    broadcaster.tools.append(PanTool(plot2))

    for c in (plot1, plot2):
        zoom = ZoomTool(component=c, tool_mode="box", always_on=False)
        broadcaster.tools.append(zoom)

    plot1.tools.append(broadcaster)

    return plot1

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

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

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

    def _plot_default(self):
         return _create_plot_component()

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

回答1:


One of the warts in Chaco (and indeed many plotting libraries) is the overloading of terms---especially the word "plot".

You're creating two different (capital-"P") Plots, but (I believe) you really only want one. Plot is the container that holds all of your individual line ... umm ... plots. The Plot.plot method returns a list of LinePlot instances (this "plot" is also called a "renderer" sometimes). That renderer is what you want to add to your (capital-"P") Plot container. The plot method actually creates the LinePlot instance and adds it to the Plot container for you. (Yup, that's three different uses of "plot": The container, the renderer, and the method on the container that adds/returns the renderer.)

Here's a simpler version of _create_plot_component that does roughly what you want. Note that only a single (capital-"P") Plot container is created.

def _create_plot_component():
    # Create some x-y data series to plot
    x = linspace(-2.0, 10.0, 100)
    x2 =linspace(-5.0, 10.0, 100)

    pd = ArrayPlotData(x=x, x2=x2)
    for i in range(3):
        pd.set_data("y" + str(i), jn(i,x))

    # slightly different plot data
    for i in range(3, 5):
        pd.set_data("y" + str(i), 2*jn(i,x2))

    # Create some line plots of some of the data
    canvas = Plot(pd)
    canvas.plot(("x", "y0", "y1", "y2"), name="plot 1", color="red")
    canvas.plot(("x2", "y3", "y4"), name="plot 2", color="green")
    return canvas

Edit: An earlier response fixed the issue with a two-line modification, but it wasn't the ideal way to solve the problem.



来源:https://stackoverflow.com/questions/23843535/chaco-getting-multiple-data-series-to-use-the-same-axes-and-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!