问题描述
我正在尝试创建多线图以显示来自2D NumPy数组的多个时间序列数据(电压).我已经开始非常简单地尝试从2x10阵列中绘制带有十个数据点的两条线,但是如果没有大量无法调试的错误输出,我什至无法使它工作.
I am trying to create a multi line plot to display multiple time series data (voltages) from a 2D NumPy array. I have started very simply trying to plot two lines with ten data points from a 2x10 array, but I am not even able to get this to work without getting a large amount of error output that I am unable to debug.
进口:
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内核,并且此错误显示在外壳中:
Then I get a window appear but that hangs and crashes the Python kernel and this error displayed in the 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文档:
I don't really know what this means. I have read and re-read the API documentation at:
http://docs.enthought.com/chaco/api/renderers. html#multilineplot
以及位于以下位置的用户指南文档:
as well as the user guide documents at:
http://docs.enthought.com/chaco/user_manual/plot_types.html#multi-line-plot
,但是关于这个Class似乎没有任何其他文档.我想知道它是否未维护并且可能损坏或者我做错了什么(我很可能是因为我仅使用Chaco大约1周,并且该库对我来说是新的,就像Python中的OOP一样) ).
but there doesn't seem to be any other documentation on this Class. I am wondering if it is not maintained and may be broken or if I am doing something wrong (I may well be as I have only been using Chaco for about 1 week and the library is new to me, as is OOP in Python in general).
在此先感谢您的帮助.
推荐答案
不确定要镜像的示例,但是直接使用DataSource实例并不是从Chaco开始的最简单方法.
Not sure what example you were mirroring, but working directly with DataSource instances isn't the easiest way to start with Chaco.
我的建议是使用常规的Plot
类和ArrayPlotData
类来存储数组.展开一点,并假设您要在此处绘制多个时间序列,这是一个工作示例,其中包含具有不同颜色的多个线图:
My suggestion is to use the regular Plot
class and the ArrayPlotData
class to store your arrays. Expanding a bit, and assuming you have multiple timeseries to plot here is a working example with multiple line plots with different colors:
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()
这篇关于Chaco MultiLinePlot-无法显示简单的绘图,想知道包装是否破损?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!