Reputation: 473
When fig.canvas.draw() is used in onclick function, it doesn't wait for onclick event, and it comes out of the function. How to make it work continuously, so that label can be displayed every time when clicked on the pie chart.
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()
Upvotes: 0
Views: 260
Reputation: 7842
Your problem is in the onclick
function.
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()
Here you are creating new wedges
(which overwrite your old wedges
instances) and you are not setting them to be pickable. A quick patch for this is to change onclick
to:
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)
Upvotes: 1