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')
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()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With