简体   繁体   English

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

[英]Python - How to hide labels and keep legends matplotlib?

I would like to remove the labels of a pie chart and keep the legends only. 我想删除饼图的标签并仅保留图例。 Currently, my code has both. 目前,我的代码兼有。 Any idea how to remove the labels? 任何想法如何删除标签?

I've tried the code below: 我试过下面的代码:

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

and 

labels=None 

Bu didn't work. 卜没工作

My full code is: 我的完整代码是:

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)

Thanks for your input 感谢您的输入

If you alter your code to the following, it should remove the labels and keep the legend: 如果将代码更改为以下代码,则应删除标签并保留图例:

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)

If I understand your question correctly this should solve it. 如果我正确理解您的问题,则应该解决。 Removing the labels from the pie chart creation and adding the labels to the legend - 从饼图创建中删除标签,并将标签添加到图例-

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM