how to align axis label to the right or top in matplotlib

As of v3.3 you can now set these with the loc argument of set_xlabel and set_ylabel:

import matplotlib.pyplot as plt

# Left plot
options = ['left', 'center', 'right']
fig, axs = plt.subplots(len(options), 1, constrained_layout=True)

for ax, loc in zip(axs, options):
    ax.plot([1, 2, 3])
    ax.set_xlabel(f'xlabel loc={loc!r}', loc=loc)
# Right plot
options = ['bottom', 'center', 'top']
fig, axs = plt.subplots(1, len(options), constrained_layout=True)

for ax, loc in zip(axs, options):
    ax.plot([1, 2, 3])
    ax.set_ylabel(f'ylabel loc={loc!r}', loc=loc)
enter image description here enter image description here

An ad hoc solution, that introduces the magic value 25 that I don't understand very much, to say the least, is

plt.xlabel('pollo', horizontalalignment='right', position=(1,25))

A more sensible approach is to use whichever y position the axes.xaxis has choosen... With this idea in mind a sane procedure is usually like

  1. get the object using a .get_object(...) method of the parent container
  2. modify the object
  3. update the parent using its .set_object(...) method

In our case, we make the plot, then we get the current axis ax that contains the xaxis that contains the label whose position and alignment we want to modify.

...
plt.xlabel('...')
...
ax = plt.gca()
label = ax.xaxis.get_label()
x_lab_pos, y_lab_pos = label.get_position()
label.set_position([1.0, y_lab_pos])
label.set_horizontalalignment('right')
ax.xaxis.set_label(label)
...
plt.show()

With respect of acting directly on matplotlib's defaults, I've browsed the plt.rcParams data structure but I haven't spotted anything useful. Of course that doesn't mean that it is impossible, just that I cannot see a viable solution.


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...