Reputation: 171
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!
Upvotes: 1
Views: 880
Reputation: 36735
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()
Upvotes: 1