0

I'm doing a bar plot with long labels, which I've rotated 45 degrees and set to be right-aligned. However, the tick labels are still a bit away from the ticks, making the plot look strange. How do move all the labels a few points to the right?

Here is my current code:

import seaborn as sns
import pylab as plt
plt.figure()
ax = sns.barplot(x="item", y="dist", hue="dset", data=df)
plt.xticks(rotation=45, ha='right')
plt.tight_layout()

EDIT: Please take look at the right-aligned subplot in stackoverflow.com/a/14854007/1452257 for an example. I can also copy the code/image to this post if you prefer.

pir
  • 5,513
  • 12
  • 63
  • 101
  • 2
    Can you show a screenshot of the plot you are getting? Also, can you create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – DavidG Dec 15 '17 at 15:50
  • It basically looks like the right-aligned plot here (https://stackoverflow.com/a/14854007/1452257), but just with longer labels. I want to move the labels a few pixels to the right so it's not the very end rightmost part of the label that's aligned with the tick, but the center of the last character. – pir Dec 15 '17 at 15:53
  • 1
    Do you mean `va='top'`? – ImportanceOfBeingErnest Dec 15 '17 at 16:11
  • Thanks. I just tried `va='top'`, but saw no noticeable difference. Suppose I wanted to move the axis labels slightly for some other purpose - isn't there an easy way to do that? – pir Dec 15 '17 at 16:33
  • 1
    A lot is possible. But it's not really clear what you mean. The right-align plot in the link looks fine. I'm repeating the first comment here, but without a [mcve] and a picture and a clear description what is wrong with that plot it's all guessing here. – ImportanceOfBeingErnest Dec 15 '17 at 16:39
  • @pir, take a look at this answer and see if that would be of help to you too: https://stackoverflow.com/a/48326438/1144382 – Thomas Fauskanger Jan 18 '18 at 17:03
  • 1
    It did not occur to me that [this](https://stackoverflow.com/a/48326438/1144382) was the desired outcome here. I guess we can mark it as duplicate of the much clearer question. – ImportanceOfBeingErnest Jan 19 '18 at 09:29

1 Answers1

2

From ImportanceOfBeingErnest's answer here, I got the following on translating tick labels in a general, arbitrary way:

import matplotlib.transforms as mtrans
# ...
trans = mtrans.Affine2D().translate(20, 0)
for t in ax.get_xticklabels():
    t.set_transform(t.get_transform()+trans)
Thomas Fauskanger
  • 2,536
  • 1
  • 27
  • 42