Reputation: 1114
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
Upvotes: 1
Views: 5041
Reputation: 2334
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)
Upvotes: 2
Reputation: 6181
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)
Upvotes: 0