简体   繁体   English

更新Chaco HPlotContainer组件的数据

[英]Updating data for a component of Chaco HPlotContainer

I have an HPlotContainer with 2 empty LinePlots as defaults, created using create_line_plot() factory function. 我有一个HPlotContainer,默认为2个空LinePlots,使用create_line_plot()工厂函数创建。 I then perform some calculations and would like to update the plots. 然后,我执行一些计算,并希望更新图表。 How do I access the ArrayPlotData of a LinePlot? 如何访问LinePlot的ArrayPlotData? I have tried something like container.components[0].data and I got an 我尝试过像container.components[0].data这样的东西

AttributeError: 'LinePlot' object has no attribute 'data' AttributeError:'LinePlot'对象没有属性'data'

Clearly I am not doing the right thing here. 显然我在这里做的不对。

The LinePlot object you are manipulating is actually what Chaco calls a "renderer" and doesn't have access to the data. 您正在操作的LinePlot对象实际上是Chaco称之为“渲染器”并且无法访问数据的对象。 To update the plots dynamically, you want to call the set_data method on the ArrayPlotData object. 要动态更新绘图,您需要在ArrayPlotData对象上调用set_data方法。 You can access that on the Plot objects, but in your case it makes most sense to keep a reference directly to the ArrayPlotData object. 您可以在Plot对象上访问它,但在您的情况下,最好将引用直接保存到ArrayPlotData对象。 If you want to update the LinePlot object then make a reference to it. 如果要更新LinePlot对象,请对其进行引用。 This example shows the standard way to do that kind of thing with TraitsUI and Chaco: 这个例子展示了使用TraitsUI和Chaco做这种事情的标准方法:

from chaco.api import ArrayPlotData, HPlotContainer, Plot, LinePlot
from enable.api import ComponentEditor
import numpy as np
from traits.api import Array, Event, HasTraits, Instance, Int
from traitsui.api import ButtonEditor, Item, View


class DataUpdateDemo(HasTraits):
    plots = Instance(HPlotContainer)
    plot_data = Instance(ArrayPlotData)
    line_plot_1 = Instance(LinePlot)
    line_plot_2 = Instance(LinePlot)
    x = Array
    y = Array
    go = Event
    w = Int(1)

    def _x_default(self):
        x = np.linspace(-np.pi, np.pi, 100)
        return x

    def _y_default(self):
        y = np.sin(self.w * self.x)
        return y

    def _plots_default(self):
        self.plot_data = ArrayPlotData(y=self.y, x=self.x)
        plot1 = Plot(self.plot_data)
        self.line_plot_renderer1 = plot1.plot(('x', 'y'), kind='line')[0]
        plot2 = Plot(self.plot_data)
        self.line_plot_renderer_2 = plot2.plot(('y', 'x'), kind='line')[0]
        plots = HPlotContainer(plot1, plot2)
        return plots

    def _go_fired(self):
        self.w += 1
        y = np.sin(self.w * self.x)
        self.plot_data.set_data("y", y)

    traits_view = View(
        Item('plots', editor=ComponentEditor(), show_label=False),
        Item('go', editor=ButtonEditor(label="update"), show_label=False),
    )


if __name__ == "__main__":
    dud = DataUpdateDemo()
    dud.configure_traits()

Now you can do whatever manipulations you want on the LinePlot object. 现在,您可以对LinePlot对象执行任何操作。

I get output that looks like this: 我得到的输出看起来像这样:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM