How do I use matplotlib autopct?

后端 未结 7 1667
野的像风
野的像风 2020-12-12 11:59

I\'d like to create a matplotlib pie chart which has the value of each wedge written on top of the wedge.

The documentation suggests I should use autopct

7条回答
  •  不知归路
    2020-12-12 12:23

    With the help of matplotlib gallary and hints from StackOverflow users, I came up with the following pie chart. the autopct shows amounts and kinds of ingredients.

    import matplotlib.pyplot as plt
    %matplotlib inline
    
    reciepe= ["480g Flour", "50g Eggs", "90g Sugar"]
    amt=[int(x.split('g ')[0]) for x in reciepe]
    ing=[x.split()[-1] for x in reciepe]
    fig, ax=plt.subplots(figsize=(5,5), subplot_kw=dict(aspect='equal'))
    wadges, text, autotext=ax.pie(amt, labels=ing, startangle=90,
                                  autopct=lambda p:"{:.0f}g\n({:.1f})%".format(p*sum(amt)/100, p),
                                  textprops=dict(color='k', weight='bold', fontsize=8))
    ax.legend(wadges, ing,title='Ingredents', loc='best', bbox_to_anchor=(0.35,0.85,0,0))
    

    Piechart showing the amount and of percent of a sample recipe ingredients

    Pie chart showing the salary and percent of programming Language users

提交回复
热议问题