1

I'm trying to move axes labels in matplotlib. I thought this would work but it doesn't:

import matplotlib.pyplot as plt plt.figure(0) xlbl = plt.xlabel("foo") pos = xlbl.get_position() pos = (pos[0], pos[1] + 1) xlbl.set_position(pos) plt.draw() 

However, this does work (moving in x as opposed to y):

xlbl = plt.xlabel("foo") pos = xlbl.get_position() pos = (pos[0]+1, pos[1]) xlbl.set_position(pos) plt.draw() 

I've searched everywhere and can only find a solution involving rcParams. This is an undesired solution because it affects all labels in my graph. I would like to move just one label.

thanks!

1 Answer 1

1

Try to use set_label_coords:

import matplotlib.pyplot as plt plt.figure(0) xlbl = plt.xlabel("foo") pos = xlbl.get_position() pos = (pos[0]+0.3, pos[1]+0.5) ax = plt.gca() ax.xaxis.set_label_coords(pos[0], pos[1]) plt.draw() plt.show() 

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

the positions it takes is not the same as the ones returned by xlbl.get_position() (seems to be relative axes coordinates, I think...) but by tweaking I got it to work, thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.