无法将autopct添加到axis.pie ::错误:值太多

Jry*_*972 2 python matplotlib pie-chart

在以下代码中,只要鼠标单击饼图,我就在控制台上打印标签。问题是由于楔块的原因,我无法将autopct添加到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()
Run Code Online (Sandbox Code Playgroud)

CT *_*Zhu 7

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

juck将成为text.Text您的百​​分比值的对象。 在此处输入图片说明