2
votes

I am having a question regarding a pie-chart labels alignment. I'd like to have labels outside the pie-chart and centered to each wedge. According to the documentation page, "labeldistance" parameter enables to place labels outside the pie-chart, and "ha" & "va" parameters supposed to center. However, these two options (ha & va) seems not working on Matplotlib v2.1.0+. 1) With this example (pls see below), you can see that "car" label is not centered properly, it is a bit off the center.

import matplotlib.pyplot as plt
figure = plt.figure()
axes = figure.add_subplot(111)
axes.set_aspect(1)  # circular pie
y = [1,2,3, 4,8,16,18]
label = ['car','domino', 'romancical','testing1', 'thisisthelonglabel', 
         'fffffffffffffffffffffffffffffffffffffffffff', 'as']
wedges, texts = plt.pie(y, 
                        radius=1.2, 
                        labels=label, 
                        labeldistance=1.0, 
                        rotatelabels =True,
                        startangle = 10,
                        wedgeprops = {"linewidth": 0.7, 
                                      "edgecolor": "white"},
                        textprops = dict(ha="center", 
                                          va="center")) # doesn't work 
plt.show()

enter image description here

I added the following lines to force labels to be centered, which works but disabled "labeldistance" parameter. So all my centered correctly, as I want labels are overlapping with the pie-chart circle.

    wedges, texts = plt.pie(y, 
                            radius=1.2, 
                            labels=label, 
                            labeldistance=1.0, 
                            rotatelabels =True,
                            startangle = 10,
                            wedgeprops = {"linewidth": 0.7, 
                                          "edgecolor": "white"},
                            textprops = dict(ha="center", 
                                              va="center"))
for t in texts:
       t.set_horizontalalignment("center")
       t.set_verticalalignment("center")
 plt.show()    

enter image description here

So my question is, do "ha" & "va" options work for other users? And could anyone advice if there is a solution for keeping "labeldistance" while using .set_horizontalalignment("center") and set_verticalalignment("center") ?

Thank you.

1
You are setting both horizontal and vertical alignments to 'center', but what you want is to only set the vertical alignment to 'center'. In other words, remove the lines ha="center" and t.set_horizontalalignment("center"). If anything, set the horizontal alignment to 'left'.Thomas Kühn
See also this, it may be related.Thomas Kühn
For me, on 2.2.2, both your codes generate the second figureSheldore
@ThomasKühn: Hiding ha or setting it to left also doesn't seem to workSheldore
@Bazingaa I only have Maplotlib 3.0.2 available.Thomas Kühn

1 Answers

4
votes

In matplotlib 3.0.2 as well as 2.1.2, using

textprops = dict(va="center", rotation_mode = 'anchor')

(and labeldistance=1.05) results in

enter image description here

Note that this leaves out the ha="center" option, because the horizontal alignment is best be automatically set depending on if the label is on the left or right side of the circle.

For an explanation of rotation_mode see e.g. this question or this question.