2016-09-07 131 views
0

我想删除饼图的标签并只保留图例。目前,我的代码都有。任何想法如何删除标签?Python - 如何隐藏标签并保持传说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) 

感谢您的输入

回答

1

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) 
0

如果我明白你的问题正确这应该解决这个问题。 去除饼图创建标签并添加标签的传说 - 如果你改变你的代码下面,应该删除标签,并保持传说

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) 
+0

谢谢但实际上你的建议标签=国家不起作用。仍然可见的标签和图例。 – Gonzalo

+0

这是否适用于您(没有标签,图例存在) - ' import matplotlib.pyplot as plt #切片将被排序并逆时针绘制。 labels ='Frogs','Hogs','Dogs','Logs' sizes = [15,30,45,10] colors = ['yellowgreen','gold','lightskyblue','lightcoral'] 爆炸=(0,0.1,0,0)#仅“爆炸”第二个切片(即'Hogs') plt.pie(尺寸,爆炸=爆炸,颜色=颜色, autopct ='%1.1f% %',shadow = True,startangle = 90) plt.legend(loc =“best”,labels = labels) plt.show() ' –

+0

我发现我可以实现只是一个传说,没有标签,如果我或者省略'plt.pie'调用的'labels'参数或者设置'labels = None',然后调用'plt.legend(loc =“best”,labels = country)'。这不适合你吗? –