Quantcast
Channel: How to align axis label to the right or top in matplotlib? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

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

$
0
0

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.


Viewing all articles
Browse latest Browse all 4

Trending Articles



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