2

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.

K.Ivi
  • 111
  • 2
  • 9
  • 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 Jan 30 '19 at 13:36
  • 1
    See also [this](https://stackoverflow.com/a/52027010/2454357), it may be related. – Thomas Kühn Jan 30 '19 at 13:38
  • 1
    For me, on 2.2.2, both your codes generate the second figure – Sheldore Jan 30 '19 at 13:38
  • 1
    @ThomasKühn: Hiding `ha` or setting it to left also doesn't seem to work – Sheldore Jan 30 '19 at 13:42
  • @Bazingaa I only have Maplotlib 3.0.2 available. – Thomas Kühn Jan 30 '19 at 13:47
  • @Thomas Kühn: the output remains the same even if I remove set_horizontalalignment & ha options. Thanks for the link , I already went through it thoroughly. It makes me think that this issue is specifically related to Matplotlib v2.1.0 – K.Ivi Jan 30 '19 at 13:52

1 Answers1

4

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.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712