How to add a legend to matplotlib pie chart?

前端 未结 2 1264
予麋鹿
予麋鹿 2020-12-10 05:34

Using this example http://matplotlib.org/examples/pie_and_polar_charts/pie_demo_features.html how could I add a legend to this pie chart? My problem is that I have One big

2条回答
  •  一向
    一向 (楼主)
    2020-12-10 05:58

    You can change your legend following types-

    best
    upper right
    upper left
    lower left
    lower right
    right
    center left
    center right
    lower center
    upper center
    center
    
    state = stateData['State/UnionTerritory']
    cases = stateData['ConfirmedIndianNational']
    explode = stateData.ConfirmedIndianNational.apply(lambda x:x > 100)
    explode = explode.apply(lambda x:0.2 if x == True else 0)
    plt.title("Covid 19")
    plt.pie(cases, explode=explode,autopct='%1.2f%%',shadow=True, radius=3)
    plt.legend(state, loc="center")
    plt.show()
    

提交回复
热议问题