Quantcast
Viewing latest article 2
Browse Latest Browse All 4

Answer by gboffi for How to align axis label to the right or top in matplotlib?

UPDATE

Current 2023's Matplotlib has a new keyword loc=… for plt.[xy]label.
From xlabel doc string

loc : {'left', 'center', 'right'},
The label position. This is a high-level alternative for passing
parameters *x* and *horizontalalignment*.

and you can write simply

plt.xlabel('x_description', loc='right')plt.ylabel('y_description', loc='top')

Image may be NSFW.
Clik here to view.
enter image description here


My other answer is still a good one, because the idea of getting an object, modifying it and setting it back is a good idea on its own, but here it is an alternative, cleaner solution:

...plt.xlabel('x_description', horizontalalignment='right', x=1.0)plt.ylabel('y_description', horizontalalignment='right', y=1.0)...

as you can see, no more magic numbers, and works both for xlabel and ylabel.

Note that in both cases we are going to change the horizontal alignment, for reasons that were eventually clear to me when I first changed the vertical alignment in ylabel...


Viewing latest article 2
Browse Latest Browse All 4

Trending Articles



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