Python - 如何隐藏标签并保留图例 matplotlib?

Python - How to hide labels and keep legends matplotlib?

我想删除饼图的标签,只保留图例。目前,我的代码都有。知道如何删除标签吗?

我试过下面的代码:

plt.legend(labels, loc="best") 

and 

labels=None 

布没用。

我的完整代码是:

plt.pie(percent,              # data
    explode=explode,    # offset parameters 
    labels=country,      # slice labels
    colors=colors,      # array of colours
    autopct='%1.0f%%',  # print the values inside the wedges - add % to the values 
    shadow=True,        # enable shadow
    startangle=70       # starting angle
    )

plt.axis('equal')
plt.title('Top 5 Countries', y=1.05, fontsize=15) #distance from plot and size
plt.legend( loc="best") 
plt.tight_layout()

countrypie = "%s_country_pie.png" % pname
plt.savefig(countrypie)

感谢您的意见

如果我正确理解你的问题,这应该可以解决问题。 从饼图创建中删除标签并将标签添加到图例 -

plt.pie(percent,              # data
    explode=explode,    # offset parameters 
    colors=colors,      # array of colours
    autopct='%1.0f%%',  # print the values inside the wedges - add % to the values 
    shadow=True,        # enable shadow
    startangle=70       # starting angle
    )

plt.axis('equal')
plt.title('Top 5 Countries', y=1.05, fontsize=15) #distance from plot and size
plt.legend(loc="best", labels=country) 
plt.tight_layout()

countrypie = "%s_country_pie.png" % pname
plt.savefig(countrypie)

如果您将代码更改为以下内容,它应该删除标签并保留图例:

plt.pie(percent,              # data
    explode=explode,    # offset parameters 
    labels=None,      # OR omit this argument altogether
    colors=colors,      # array of colours
    autopct='%1.0f%%',  # print the values inside the wedges - add % to the values 
    shadow=True,        # enable shadow
    startangle=70       # starting angle
)

plt.axis('equal')
plt.title('Top 5 Countries', y=1.05, fontsize=15) #distance from plot and size
plt.legend( loc="best", labels=country) 
plt.tight_layout()

countrypie = "%s_country_pie.png" % pname
plt.savefig(countrypie)