In the following code, i'm just printing the label on the console whenever mouse clicks on the pie chart. The problem is i cant add autopct to the ax.pie() because of the wedges thing, i don't know how to add percentage label on the piechart without using autopct to ax.pie.
import matplotlib.pyplot as plt
labels = ['Beans', 'Squash', 'Corn']
i=0
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):
global i
def onclick(event):
global i
i=i+1
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)# how to add autopct='%1.1f%%'
fig.canvas.draw()
for wedge in wedges:
wedge.set_picker(True)
# Make wedges selectable
if i==0:
for wedge in wedges:
wedge.set_picker(True)
fig.canvas.mpl_connect('pick_event', onclick)
if __name__ == '__main__':
main()
If you want to use autopct
, remember now you have 3 values to be unpacked instead of two, so change your code to wedges, plt_labels, junk = ax.pie([20, 40, 60], labels=labels, autopct='%1.1f%%')
will solve your problem
juck
is going to be the text.Text
objects of your percentage values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With