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()
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()
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.
ha="center"
andt.set_horizontalalignment("center")
. If anything, set the horizontal alignment to 'left'. – Thomas Kühnha
or setting it to left also doesn't seem to work – Sheldore