Chaco MultiLinePlot - unable to get simple plot to display, wondering if package broken?

风格不统一 提交于 2019-12-06 11:19:01

问题


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.

Imports:

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:

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

Display Class:

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

Create Instance of Class:

my_graph = Multi_line_graph(test_array)

Display (configure traits):

my_graph.configure_traits()

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.

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

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).

Many thanks in advance for any help..


回答1:


Not sure what example you were mirroring, but working directly with DataSource instances isn't the easiest way to start with Chaco.

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()


来源:https://stackoverflow.com/questions/23015052/chaco-multilineplot-unable-to-get-simple-plot-to-display-wondering-if-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!