Quantcast
Channel: Aligning rotated xticklabels with their respective xticks - Stack Overflow
Viewing all articles
Browse latest Browse all 7

Answer by Jeffrey Benjamin Brown for Aligning rotated xticklabels with their respective xticks

$
0
0

An easy, loop-free alternative is to use the horizontalalignment Text property as a keyword argument to xticks[1]. In the below, at the commented line, I've forced the xticks alignment to be "right".

n=5x = np.arange(n)y = np.sin(np.linspace(-3,3,n))xlabels = ['Long ticklabel %i' % i for i in range(n)]fig, ax = plt.subplots()ax.plot(x,y, 'o-')plt.xticks(        [0,1,2,3,4],        ["this label extends way past the figure's left boundary","bad motorfinger", "green", "in the age of octopus diplomacy", "x"],         rotation=45,        horizontalalignment="right")    # hereplt.show()

(yticks already aligns the right edge with the tick by default, but for xticks the default appears to be "center".)

[1] You find that described in the xticks documentation if you search for the phrase "Text properties".


Viewing all articles
Browse latest Browse all 7

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>