无法将autopct添加到axis.pie :: 错误:值太多
在下面的代码中,每当鼠标点击饼图时,我只是把标签打印到控制台上。问题是我不能在ax.pie()中添加自动百分比标签,因为涉及到饼图的切片。我不知道如何在饼图上添加百分比标签,而不使用ax.pie中的autopct。
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()
1 个回答
11
如果你想使用 autopct
,记住现在你需要解包三个值,而不是两个。所以把你的代码改成 wedges, plt_labels, junk = ax.pie([20, 40, 60], labels=labels, autopct='%1.1f%%')
这样就能解决你的问题。
junk
将会是你百分比值的 text.Text
对象。
