Python 交互式绘图Chaco,特性通知中出现异常

Python 交互式绘图Chaco,特性通知中出现异常,python,enthought,chaco,Python,Enthought,Chaco,我在查科图书馆工作。我制作了一个交互式绘图,在这个绘图中,使用chaco.tools.api RangeSelection,我可以选择其中的一部分。现在,我想知道这个长方形在什么位置。使用下面的代码,我可以做我需要的所有事情,但我有以下错误,为什么??在错误中,指向I nedd的点是选择屏蔽 代码如下: # Major library imports from numpy import arange from scipy.special import jn # Enthought librar

我在查科图书馆工作。我制作了一个交互式绘图,在这个绘图中,使用chaco.tools.api RangeSelection,我可以选择其中的一部分。现在,我想知道这个长方形在什么位置。使用下面的代码,我可以做我需要的所有事情,但我有以下错误,为什么??在错误中,指向I nedd的点是选择屏蔽

代码如下:

# Major library imports
from numpy import arange
from scipy.special import jn

# Enthought library imports
from enable.api import Component, ComponentEditor
from traits.api import HasTraits, Instance, Any, on_trait_change
from traitsui.api import Item, Group, View

# Chaco imports
from chaco.api import create_line_plot, add_default_axes, add_default_grids
from chaco.tools.api import RangeSelection, RangeSelectionOverlay

# # Create the Chaco plot.
def _create_plot_component(self):

    numpoints = 100
    low = -5
    high = 15.001
    x = arange(low, high, (high-low)/numpoints)

    # Plot a bessel function
    y = jn(0, x)
    plot = create_line_plot((x,y), color=(0,0,1,1), width=2.0, index_sort="ascending")
    plot.active_tool = RangeSelection(plot, left_button_selects = True,     auto_handle_event = False)
    plot.overlays.append(RangeSelectionOverlay(component=plot))
    plot.bgcolor = "white"
    plot.padding = 50
    add_default_grids(plot)
    add_default_axes(plot)
    self.times_ds = plot.index
    self.times_ds.on_trait_change(self._selections_changed, 'metadata_changed')

    return plot

# Attributes to use for the plot view.
size=(600,500)
title="Simple line plot"

class Demo(HasTraits):
    times_ds = Any()
    plot = Instance(Component)
    corr_renderer = Any()

    traits_view = View(
                    Group(
                        Item('plot', editor=ComponentEditor(size=size),
                             show_label=False),
                        orientation = "vertical"),
                    resizable=True, title=title,
                    width=size[0], height=size[1]
                    )

    def _plot_default(self):
         return _create_plot_component(self)

    def _selections_changed(self, event):
        selections = event["selections"]
        low, high = selections
        print low, high

demo = Demo()
if __name__ == "__main__":
    demo.configure_traits()
这是误差的一部分,误差是矩形的点

Exception occurred in traits notification handler for object:   <chaco.array_data_source.ArrayDataSource object at 0x66a94d0>, trait: metadata_changed, old  value: <undefined>, new value: True
Traceback (most recent call last):
  File "/home/franco/Python/Canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/site-packages/traits/trait_notifiers.py", line 521, in rebind_call_1
    self.dispatch( getattr( obj(), self.name ), new )
  File "/home/franco/Python/Canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/site-packages/traits/trait_notifiers.py", line 455, in dispatch
handler( *args )
  File "/home/franco/Desktop/Proyecto/Codigo/analisis_mas_range_relections.py", line 59, in _selections_changed
    selections = event["selections"]
TypeError: 'bool' object has no attribute '__getitem__'
Exception occurred in traits notification handler for object:  <chaco.array_data_source.ArrayDataSource object at 0x66a94d0>, trait: metadata_changed, old  value: <undefined>, new value: True

#the error continue but is the same error change the selection_masks....

TypeError: 'bool' object has no attribute '__getitem__'
Exception occurred in traits notification handler for object:  <chaco.array_data_source.ArrayDataSource object at 0x6758290>, trait: metadata_changed, old  value: <undefined>, new value: {'selection_masks': (-0.5953709619238516, 10.991581102204394)}

#the selection_masks are the point to I need

谢谢

我不确定第一次回溯是从哪里来的,因为它似乎引用了这里没有发布的代码。然而,第二个回溯是明确的


元数据更改事件未被分配元数据字典。至少,不总是这样。多年来,修改元数据的工具编写得不一致。直接从数据源获取元数据字典即可。

我认为您发布的代码不是您运行的代码。您发布的代码中没有与第一个回溯中的行对应的行:corr_index=self.corr_renderer.index。你能澄清一下这是怎么回事吗?罗伯特,我把一个旧代码的错误放进去了。我编辑我的问题。感谢Robert,我是Chaco的一个新用户,我不能很好地理解元数据字典。我在哪里可以知道这件事??或者我在哪里可以看到一些例子??感谢您的时间和帮助。self.times\u ds.metadata是您需要查看的元数据字典。只需在您的_selections _changed方法中使用它而不是事件,您可以将其更改为无参数。元数据字典将有一个“选择”条目,其中包含您要查看的内容。谢谢您的帮助。要运行上述代码,只需更改以下sel_index=self.times\ds.metadata.get'selections',[]在def\u selections\u changedself:1个问题。谁能用我想要的键写呢?抱歉,我不能解析这个问题。您是否在询问如何使RangeSelection写入与默认“selections”不同的键?将其metadata\u name trait设置为所需的键。