Matplotlib Center Alignment For Pie Chart Labels
Solution 1:
You can pass a dictionary of text properties to plt.pie
via the textprops
argument. For example:
plt.pie(k, labels=labels, textprops={'weight': 'bold'})
However, if you try to specify the horizontalalignment
property, you'll get an error saying that you provided that parameter twice. Obviously you didn't, but matplotlib passed both it's hard-coded value and your value to some internal function.
But that's probably a good thing. The way I see it, there's not so much a mix of alignments, but a consistent alignment of the text against the pie.
Back to your question
pie
returns both the patches and the labels for each wedge. So you can loop through the labels after your initial call to pie
to modify their alignment. That looks like this:
k = [7, 15]
labels = 'Strongly and Mostly\nAgree', 'Strongly/Mostly Disagree\nand In the Middle'
fig, ax = plt.subplots()
ax.set_aspect('equal')
wedges, labels = ax.pie(k, labels=labels, textprops={'weight': 'bold'})
forlabelin labels:
label.set_horizontalalignment('center')
As you can see, the labels now overlap with the wedges, diminishing legibility.
The labels also have a set_position
method (i.e., label.set_position((x, y))
), but recomputing the positions for N labels in a pie chart sounds like a Bad Time to me.
Post a Comment for "Matplotlib Center Alignment For Pie Chart Labels"