2

I am trying to rotate the xticks labels in a pandas bar plot.

My problem is that when I do so, the xticks labels are centered according to their middle, and I would like to center the otherwise (so that their top extremity is centered with the corresponding bar).

Any idea how to do that?

Here is an example, first without the rotation:

foo = pd.Series({'A first label':4,'A very long yet absolutely necessary label':10,'A short label':12,
            'Another label':6,"Getting long":3,"You weren't supposed to read this'":7,
            "42 obviously":8,"Blank page syndrom":2})
foo.plot(kind = 'bar')

Without rotation

And then when I add the rotation, the long labels don't behave the way I'd want:

With rotation

See for instance the A very long yet absolutely necessary label which is almost above the Another label on its right.

I would like the end of each label to be centered with the bar above it, how can I do this?

ysearka
  • 3,805
  • 5
  • 20
  • 41
  • How comes I didn't find this question when I searched. My bad! Thanks for the redirection. Though I wonder if there is a way to do it using an option in `pandas.Series.plot`, you wouldn't happen to know this? Plus when I do `center = right` which is what I'm looking for, it seems the labels are a little on the left compared to the bars, any way to correct this? – ysearka Aug 27 '18 at 09:06
  • 2
    try `rotation_mode='anchor'`. Find the best mix of all the keywords that suits you best - I think `plt.xticks(rotation=60, ha='center', va='center', rotation_mode='anchor')` is probably what you're after. – asongtoruin Aug 27 '18 at 11:33
  • Perfect `rotation_mode='anchor'` fits perfectly. Thank you! – ysearka Aug 27 '18 at 11:53

0 Answers0