【问题标题】:Matplotlib event picker - Inside a classMatplotlib 事件选择器 - 类内部
【发布时间】:2017-07-14 19:24:17
【问题描述】:

我正在尝试在类中插入此 python 文档中显示的事件选择器示例

http://matplotlib.org/users/event_handling.html

代码是这样的

import numpy as np
import matplotlib.pyplot as plt


class Test:
    def __init__(self,line):
        self.line = line
        self.cidpress = self.line.figure.canvas.mpl_connect('button_press_event', self.onpick)

    def onpick(self, event):
        thisline = event.artist
        xdata = thisline.get_xdata()
        ydata = thisline.get_ydata()
        ind = event.ind
        points = tuple(zip(xdata[ind], ydata[ind]))
        print('onpick points:', points)


fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click on points')

line, = ax.plot(np.random.rand(10), 'o', picker=5)  # 5 points tolerance
a = Test(line)

plt.show()

但是当鼠标点击一个点时我得到这个错误。

AttributeError: 'MouseEvent' object has no attribute 'artist'

这可能是什么原因? 当不在类内时,代码可以完美运行

非常感谢

【问题讨论】:

    标签: python events matplotlib picker


    【解决方案1】:

    我怀疑代码在类之外是否有效。您在这里面临的问题是您使用了'button_press_event',它没有artist 属性。无论在课堂内还是课堂外,这都不会改变。

    【讨论】:

    • 感谢@ImportanceOfBeingErnest。奇迹般有效。不敢相信我花了一整天的时间。
    猜你喜欢
    • 2019-06-14
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 2016-07-18
    相关资源
    最近更新 更多