petit guillaume
petit guillaume

Reputation: 21

Pie chart Python: how to put the labels in a legend box

I was wondering if it was possible to remove the labels from a pie chart on the graph but to keep them in a legend box:

import matplotlib.pyplot as plt
def plot_age(young, middle,old,n,name):

    plt.figure(n)
    sizes=[young,middle,old]
    labels='young cell(s)','middle-aged cell(s)','old cell(s)'
    colors=['pink','red','darkred']

    plt.pie(sizes,labels=labels,colors=colors,shadow=True)
    plt.axis('equal')
    plt.legend(loc=(-0.05,0.05),shadow=True)
    plt.savefig(name)

right now the labels : 'young cells'...etc. appear both on the graph and on the legend box but since the name is quite long sometimes I was wondering if it was possible to remove it from the graph.

And since I am there I would like to know if it is possible to modify the orientation of the shadow of the legend box, because I have just realized that it goes in another direction than the shadow of the pie chart. Thank you very much for your answers, best regards

Upvotes: 2

Views: 4274

Answers (1)

Provide your labels directly to the legend and don't pass them to your pie

plt.pie(sizes, colors=colors, shadow=True)
plt.axis('equal')
plt.legend(labels, loc=(-0.05, 0.05), shadow=True)
plt.savefig(name)

Upvotes: 4

Related Questions