在下面的代码中,只要鼠标单击饼图,我就在控制台上打印标签。问题是我不能将autoct添加到斧头派()因为楔子的问题,我不知道如何在不使用autoct的情况下在piechart上添加百分比标签斧头派. 在
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()
如果您想使用
autopct
,请记住现在要解压的值是3个,而不是2个,因此将代码更改为wedges, plt_labels, junk = ax.pie([20, 40, 60], labels=labels, autopct='%1.1f%%')
将解决您的问题juck
将是百分比值的text.Text
对象。相关问题 更多 >
编程相关推荐