Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align axis label to the right or top in matplotlib?

By default matplotlib plots the axis label at the center of the axis. I would like to move the label in such way that it is aligned with the end of the axis, both for the horizontal and vertical axis. For example for the horizontal axis I would like to see:

+--------------------+
|                    |
|                    |
|                    |
|                    |
|                    |
+--------------------+
                 label

Is it possibile to do it with the global setting of matplotlib?

like image 367
Ruggero Turra Avatar asked Jul 04 '16 13:07

Ruggero Turra


1 Answers

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

like image 163
gboffi Avatar answered Oct 08 '22 20:10

gboffi