matplot pie: rotate labels horizontally

前端 未结 2 769
你的背包
你的背包 2021-01-06 12:03

Using matplot to create a little gauge with the code below:

    group_size=[10,10,10,10,10,50]
    labels=[\'AAAA\',\'BBBB\',\'CCCC\',\'DDDD\',\'EEEE\',\'\'         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-06 12:19

    You need to rotate the pie labels manually. To this end you may loop over the labels and set the rotation to your needs.

    group_size=[10,10,10,10,10,50]
    labels=['AAAA','BBBB','CCCC','DDDD','EEEE','']
    fig, ax = plt.subplots()
    ax.axis('equal')
    pie = ax.pie(group_size, radius=2.2, colors=['k'] ,startangle=180,counterclock=False)
    pie2 = ax.pie([10,10,10,10,10,50], radius=2,  labeldistance=0.9, labels=labels, 
                  rotatelabels =True, startangle=180,counterclock=False)
    
    plt.setp(pie2[1], rotation_mode="anchor", ha="center", va="center")
    for tx in pie2[1]:
        rot = tx.get_rotation()
        tx.set_rotation(rot+90+(1-rot//180)*180)
    
    plt.show()
    

提交回复
热议问题