2016-09-07 99 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)'。這不適合你嗎? –