Python matplotlib中pick_事件的异常行为

Python matplotlib中pick_事件的异常行为,python,matplotlib,event-handling,Python,Matplotlib,Event Handling,当在onclick函数中使用fig.canvas.draw()时,它不等待onclick事件,而是从函数中出来。如何使其连续工作,以便每次单击饼图时都可以显示标签 import matplotlib.pyplot as plt labels = ['Beans', 'Squash', 'Corn'] def main(): # Make an example pie plot fig = plt.figure() ax = fig.add_subplot(111)

当在onclick函数中使用fig.canvas.draw()时,它不等待onclick事件,而是从函数中出来。如何使其连续工作,以便每次单击饼图时都可以显示标签

import matplotlib.pyplot as plt
labels = ['Beans', 'Squash', 'Corn']
def main():
    # Make an example pie plot
    fig = plt.figure()
    ax = fig.add_subplot(111)

    #labels = ['Beans', 'Squash', 'Corn']
    wedges, plt_labels = ax.pie([20, 40, 60], labels=labels)
    ax.axis('equal')

    make_picker(fig, wedges)
    plt.show()

def make_picker(fig, wedges):

    def onclick(event):
        print event.__class__
        wedge = event.artist
        label = wedge.get_label()
        print label
        fig.canvas.figure.clf() 
        ax=fig.add_subplot(111)
        wedges, plt_labels = ax.pie([50, 100, 60],labels=labels)
        fig.canvas.draw()

    # Make wedges selectable
    for wedge in wedges:
        wedge.set_picker(True)

    fig.canvas.mpl_connect('pick_event', onclick)

if __name__ == '__main__':
    main()

您的问题在
onclick
函数中

def onclick(event):
    print event.__class__
    wedge = event.artist
    label = wedge.get_label()
    print label
    fig.canvas.figure.clf() 
    ax=fig.add_subplot(111)
    wedges, plt_labels = ax.pie([50, 100, 60],labels=labels)
    fig.canvas.draw()
在这里,您正在创建新的
楔块
(它将覆盖旧的
楔块
实例),并且您没有将它们设置为可拾取。一个快速补丁是将
onclick
更改为:

def onclick(event):
    print event.__class__
    wedge = event.artist
    label = wedge.get_label()
    print label
    fig.canvas.figure.clf()
    ax=fig.add_subplot(111)
    wedges, plt_labels = ax.pie([50, 100, 60],labels=labels)
    fig.canvas.draw()
    for wedge in wedges:
        wedge.set_picker(True)

你能详细说明你的问题并清楚地说明你想要什么吗?如果您只想打印标签,那么为什么要打印打印打印?是的,标签不适用于更新的打印。我希望当用户单击饼图时,无论是否修改饼图的打印值,都能显示出来。非常感谢!是的,我没有注意到我正在覆盖我的旧楔子实例。首先,我必须使旧的楔子可拾取,然后必须使新的楔子实例可拾取。Thx:)