1

I have produced a very simple pie chart in Python using Matplotlib and I am wanting to edit the alignment of my labels. I have used \n within my labels to split the line as the labels are too long for one line. But as you can see from the picture called 'pie chart image', it's a mix of weird alignments at the moment. I would really like to have it center alignment.

For other chart/graph types in Matplotlib there is an argument called align where you can set it to center, however, plt.pie(...) does not seem to have this attribute.

Here is my code:

import matplotlib.pyplot as plt
k = [7,15]
labels = 'Strongly and Mostly \n Agree', 'Strongly/Mostly Disagree \n  and In the Middle'
plt.pie(k, labels= labels)
plt.show()

enter image description here

Any ideas?

Paul H
  • 65,268
  • 20
  • 159
  • 136

1 Answers1

3

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'})
for label in labels:
    label.set_horizontalalignment('center')

enter image description here

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.

Paul H
  • 65,268
  • 20
  • 159
  • 136
  • Thank you - that's very useful! I don't mind setting positions for only two labels, thanks again :) – Grace O'Halloran Mar 24 '16 at 16:11
  • Is it possible to pass `horizontalalignment` as an argument to `ax.pie`? I'm using `pandas`, which doesn't return `wedges` and `labels`; hence the question. – tommy.carstensen Aug 25 '18 at 01:17
  • @tommy.carstensen what happens when you try it? – Paul H Aug 25 '18 at 17:52
  • `horizontalalignment` is not a valid argument for `DataFrame.plot.pie()` and `Series.plot.pie()`. – tommy.carstensen Aug 25 '18 at 18:09
  • @tommy.carstensen according did you pack it into the `textprops` dictionary? – Paul H Aug 25 '18 at 22:52
  • @PaulH I just tried adding `textprops={'horizontalalignment': 'center'},` as an argument to `Series.plot.pie()`, but I oddly enough got the error message `TypeError: text() got multiple values for keyword argument 'horizontalalignment'`. I'm giving up on it for now. Please don't waste more of your time on it/me. Thanks though! – tommy.carstensen Aug 26 '18 at 12:41