无法将autoct添加到轴.饼图●错误:值太多

2024-04-20 06:43:57 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码中,只要鼠标单击饼图,我就在控制台上打印标签。问题是我不能将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()

Tags: eventaddlabelsmaindeffigplt标签
1条回答
网友
1楼 · 发布于 2024-04-20 06:43:57

如果您想使用autopct,请记住现在要解压的值是3个,而不是2个,因此将代码更改为wedges, plt_labels, junk = ax.pie([20, 40, 60], labels=labels, autopct='%1.1f%%')将解决您的问题

juck将是百分比值的text.Text对象。 enter image description here

相关问题 更多 >