首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Chaco MultiLinePlot -无法显示简单的绘图,不知道包是否损坏?

Chaco MultiLinePlot -无法显示简单的绘图,不知道包是否损坏?
EN

Stack Overflow用户
提问于 2014-04-11 22:12:19
回答 1查看 263关注 0票数 2

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

导入:

代码语言:javascript
复制
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

测试数组:

代码语言:javascript
复制
test_array = numpy.random.rand(10,2)

显示类:

代码语言:javascript
复制
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

创建类的实例:

代码语言:javascript
复制
my_graph = Multi_line_graph(test_array)

显示(配置特征):

代码语言:javascript
复制
my_graph.configure_traits()

然后我看到一个窗口出现,但它挂起并使Python内核崩溃,在shell中显示以下错误:

代码语言:javascript
复制
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

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

非常感谢您的帮助..

EN

回答 1

Stack Overflow用户

发布于 2019-09-12 02:38:03

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

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

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23015052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档