问题描述
我正在尝试在一个图上绘制多个数据集合.
I am trying to plot several collections of data on a single plot.
每个数据集都可以表示为x系列(索引)和几个y系列(值).每个数据集中x和y数据序列的范围可能不同.我想在一个绘图上显示多个这些数据集.但是,当我简单地将第二个绘图对象添加到第一个绘图对象(见下文)时,它为它创建了第二个轴,该轴嵌套在绘图内.
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()
推荐答案
Chaco(实际上还有很多绘图库)的缺点之一是术语的重载-尤其是"plot"一词.
One of the warts in Chaco (and indeed many plotting libraries) is the overloading of terms---especially the word "plot".
您要创建两个不同的(大写字母"P")Plot
,但是(我相信)您实际上只想要一个. Plot
是保存所有单独行... umm ...绘图的容器. Plot.plot
方法返回一个LinePlot
实例列表(有时也将此绘图"也称为渲染器").该渲染器就是您要添加到(大写"P")绘图"容器中的对象. plot
方法实际上创建了LinePlot
实例,并将其添加到了Plot
容器中. (是的,这是绘图"的三种不同用法:容器,渲染器以及添加/返回渲染器的容器上的方法.)
You're creating two different (capital-"P") Plot
s, 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.)
这是_create_plot_component
的简单版本,可以满足您的需求.请注意,仅创建了一个(大写字母"P")Plot
容器.
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
先前的答复通过两行修改解决了该问题,但这不是解决问题的理想方法.
An earlier response fixed the issue with a two-line modification, but it wasn't the ideal way to solve the problem.
这篇关于Chaco-获取多个数据系列以使用相同的轴和贴图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!