Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove labels but keep legend in a pie chart

How can I remove the labels from a pie chart but keep the legend ?

import matplotlib.pyplot as plt

x = [15, 30, 55]
labels = [1, 2, 3]

plt.figure(figsize=(3, 3), dpi=72)
plt.pie(x, labels=labels)
plt.legend()
plt.savefig('pie_1.png')

enter image description here

like image 475
Ghilas BELHADJ Avatar asked Feb 14 '18 16:02

Ghilas BELHADJ


2 Answers

You can remove the labels argument from the pie and add it to the legend. Instead of

plt.pie(x,labels=labels)
plt.legend()

use

plt.pie(x)
plt.legend(labels=labels)

Complete example:

import matplotlib.pyplot as plt

x = [15, 30, 55]
labels = [1, 2, 3]

plt.figure(figsize=(3, 3), dpi=72)
plt.pie(x)
plt.legend(labels=labels)
plt.show()

enter image description here

like image 143
ImportanceOfBeingErnest Avatar answered Oct 19 '22 09:10

ImportanceOfBeingErnest


As an alternative to IMCoins' answer, which is the best way to proceed, you could also keep your current code, but delete the labels from the pie chart.

x = [15, 30, 55]
labels = [1, 2, 3]

plt.figure(figsize=(3, 3), dpi=72)
patches, texts = plt.pie(x, labels=labels)
plt.legend()
for t in texts:
    t.remove()
like image 2
Diziet Asahi Avatar answered Oct 19 '22 08:10

Diziet Asahi