4

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','']
    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.7, labels=labels, rotatelabels = 270,
startangle=180,counterclock=False)
    plt.show()

I am trying to get the labels to go horizontally and align with the midpoint of each wedge like this (but with the text inside the wedges):

enter image description here

Using rotatelabel=True, I get the following:

enter image description here

Any ideas on how I can achieve the horizontal rotation of the labels for the graph?

AToe
  • 289
  • 1
  • 4
  • 12

2 Answers2

8

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()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Very helpful answer. Short question: Why does `plt.setp(pie, width=0.3, edgecolor='white')` return `TypeError: Axes.pie() got an unexpected keyword argument 'linecolor'`? – Stücke Mar 09 '23 at 07:39
  • How do I change the line colors as done in this example https://stackoverflow.com/q/55267993/5696601? – Stücke Mar 09 '23 at 07:40
3

My solution is basically similar to the accepted answer by ImportanceOfBeingErnest. But I think the steps involved are easier to grasp.

import matplotlib.pyplot as plt

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)

# ax.pie() returns wedges, labels
# Note that rotatelabels=False; so that, at this step ...
# all labels are not rotated; they will be rotated later
wedges, labels = ax.pie([10, 10, 10, 10, 10, 50], radius=2,  \
                      labeldistance=0.85, labels=labels, rotatelabels = False, \
                      startangle=180, counterclock=False)

# do the rotation of the labels
for ea, eb in zip(wedges, labels):
    mang =(ea.theta1 + ea.theta2)/2.  # get mean_angle of the wedge
    #print(mang, eb.get_rotation())
    eb.set_rotation(mang+270)         # rotate the label by (mean_angle + 270)
    eb.set_va("center")
    eb.set_ha("center")

plt.show()

The resulting image: enter image description here

swatchai
  • 17,400
  • 3
  • 39
  • 58