2

我正在尝试创建一个多线图以显示来自 2D NumPy 数组的多个时间序列数据(电压)。我已经开始非常简单地尝试用 2x10 数组中的十个数据点绘制两条线,但如果没有得到大量无法调试的错误输出,我什至无法让它工作。

进口:

import numpy
from traits.api import HasTraits, Instance
from traitsui.api import View, Item
from chaco.api import MultiLinePlot, ArrayDataSource, MultiArrayDataSource
from enable.component_editor import ComponentEditor

测试数组:

test_array = numpy.random.rand(10,2)

显示类:

class Multi_line_graph(HasTraits):

    plot = Instance(MultiLinePlot)

    traits_view = View(
    Item('plot',editor=ComponentEditor(), show_label=False),
    width=1024, height=768, resizable=True, title="EEG Preview")

    def __init__(self, my_data):
        super(Multi_line_graph, self).__init__()

        x = ArrayDataSource(numpy.arange(1, my_data.shape[0]))

        y = my_data.transpose()   #since my data columnwise
        y = MultiArrayDataSource(y)

        yidx = ArrayDataSource(numpy.arange(y.get_shape()[0]))

        plot = MultiLinePlot(index=x, yindex=yidx, value=y)

        self.plot = plot

创建类的实例:

my_graph = Multi_line_graph(test_array)

显示(配置特征):

my_graph.configure_traits()

然后我会出现一个窗口,但它会挂起并导致 Python 内核崩溃,并且在 shell 中显示此错误:

Exception occurred in traits notification handler for object: <chaco.multi_line_plot.MultiLinePlot object at 0x000000000D0CFD58>, trait: bounds_items, old value: <undefined>, new value: <traits.trait_handlers.TraitListEvent object at 0x000000000D18C908>
Traceback (most recent call last):
File "C:\Users\pzl46097\AppData\Local\Enthought\Canopy\User\lib\site-packages\traits\trait_notifiers.py", line 340, in __call__
self.handler( *args )
File "C:\Users\pzl46097\AppData\Local\Enthought\Canopy\User\lib\site-packages\chaco\base_xy_plot.py", line 613, in _bounds_items_changed
self._update_mappers()
File "C:\Users\pzl46097\AppData\Local\Enthought\Canopy\User\lib\site-packages\chaco\base_xy_plot.py", line 594, in _update_mappers
x_mapper.screen_bounds = (x, x2)
AttributeError: 'NoneType' object has no attribute 'screen_bounds'
Exception occurred in traits notification handler for object: <chaco.multi_line_plot.MultiLinePlot object at 0x000000000D0CFD58>, trait: bounds_items, old value: <undefined>, new value: <traits.trait_handlers.TraitListEvent object at 0x000000000D0C4C88>
Traceback (most recent call last):
File "C:\Users\pzl46097\AppData\Local\Enthought\Canopy\User\lib\site-packages\traits\trait_notifiers.py", line 340, in __call__
self.handler( *args )
File "C:\Users\pzl46097\AppData\Local\Enthought\Canopy\User\lib\site-packages\chaco\base_xy_plot.py", line 613, in _bounds_items_changed
self._update_mappers()
File "C:\Users\pzl46097\AppData\Local\Enthought\Canopy\User\lib\site-packages\chaco\base_xy_plot.py", line 594, in _update_mappers
x_mapper.screen_bounds = (x, x2)
AttributeError: 'NoneType' object has no attribute 'screen_bounds'
Exception occurred in traits notification handler.
Please check the log file for details.

我真的不知道这意味着什么。我已经阅读并重新阅读了 API 文档:

http://docs.enthought.com/chaco/api/renderers.html#multilineplot

以及用户指南文档:

http://docs.enthought.com/chaco/user_manual/plot_types.html#multi-line-plot

但似乎没有关于这个类的任何其他文档。我想知道它是否没有维护并且可能被破坏或者我做错了什么(我可能是因为我只使用 Chaco 大约 1 周而且这个库对我来说是新的,就像 Python 中的 OOP 一样)。

非常感谢您的帮助..

4

1 回答 1

0

不确定您要镜像哪个示例,但直接使用 DataSource 实例并不是从 Chaco 开始的最简单方法。

我的建议是使用常规Plot类和ArrayPlotData类来存储您的数组。扩展一点,假设您有多个时间序列在这里绘制是一个工作示例,其中包含多个不同颜色的线图:

import numpy 
from traits.api import Array, HasTraits, Instance 
from traitsui.api import View, Item 
from chaco.api import ArrayPlotData, Plot 
from enable.api import ComponentEditor

test_array = numpy.random.rand(10, 2)    

class Multi_line_graph(HasTraits):

    plot = Instance(Plot)

    data = Array

    traits_view = View(
        Item('plot', editor=ComponentEditor(), show_label=False),
        width=1024, height=768, resizable=True, title="EEG Preview"
    )

    def _plot_default(self):
        data = {"x": t_array[0, :], "y1": t_array[1, :], "y2": t_array[2, :]}
        plot = Plot(ArrayPlotData(**data))
        plot.plot(("x", "y1"), type="line", color="blue")
        plot.plot(("x", "y2"), type="line", color="red")
        return plot

my_graph = Multi_line_graph(data=test_array)
my_graph.configure_traits()
于 2019-09-11T18:38:03.197 回答