当在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()
答案 0 :(得分:1)
您的问题出在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()
在这里,您要创建新的wedges
(覆盖旧的wedges
个实例)并且您没有将它们设置为可选。一个快速补丁是将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)