1

I have noticed that when using Text.set_x or Text.set_y or Text.set_position() on axis labels (e.g. gca().xaxis.label), the labels are ignoring the dimension other than their main axis - i.e. when if xaxis.get_position() returns (0.5, 325), if I run xaxis.set_position(0.25, 400), only the x component updates, giving a position of (0.25, 325).

I have prepared an illustrative plot: X axis motions

Generated with the MWE below. As you can see, the labels only want to move in one direction. Apparently I can move them around with labelpad, but it seems weird to have the asymmetry and I'd be interested to know why it exists, and if there's another way I'm missing to set the axis positions to arbitrary coordinates?

The MWE code is below:

# Minimum working example demonstrating label failures
from numpy import *
from scipy import *
from matplotlib.pyplot import plot, subplot, title, figure

# Coosing an aribtrary function.
x = linspace(1, 5, 20);     y = (1/x)*cos(x);


xl = [];        yl = []    # Save the labels in an array

titles = ['Control', 'X Moved Along X, Y Moved Along X', 'X Moved along Y, Y moved Along Y', 'X Moved Along Y, Y Moved Along X']

figure(figsize=(10,10), dpi=60)
for i in range(0, 4):
    subplot(2, 2, i+1)
    plot(x, y, 'o--')
    xl.append(xlabel('x (unitless)', fontweight='semibold', fontsize=14))
    yl.append(ylabel('y (unitless)', fontweight='semibold', fontsize=14))
    title(titles[i], fontsize=12)
    xlim(1, 5)

# Only the x label moves
subplot(2, 2, 2)
xl1x = xl[1].get_position()[0]
yl1x = yl[1].get_position()[0]
xl[1].set_x(xl1x+0.2)
yl[1].set_x(yl1x+0.2)

# Only the y label moves
subplot(2, 2, 3)
xl2y = xl[2].get_position()[0]
yl2y = yl[2].get_position()[0]
xl[2].set_y(xl2y+0.2)
yl[2].set_y(yl2y+0.2)

# Neither moves
subplot(2, 2, 4)
xl3y = xl[3].get_position()[0]
yl3x = yl[3].get_position()[0]
xl[3].set_y(xl3y+0.2)
yl[3].set_x(yl3x+0.2)

tight_layout()
show()
Community
  • 1
  • 1
Paul
  • 10,381
  • 13
  • 48
  • 86
  • 2
    I can't give a full answer right now, but this is deliberate. It's a result of the transform that the axis labels use. Basically, axis labels use a blended transform where one direction is fixed in axes coordinates and the other is free to move. (The transformations tutorial is a good place to start if you're not familiar with matplotlib's transforms: http://matplotlib.org/users/transforms_tutorial.html ) At any rate, if you want to place labels at arbitrary positions, I'd recommend `annotate` over using the x-labels, y-labels, etc (though you can just change the transform of the labels). – Joe Kington Jun 25 '13 at 18:51
  • What Joe said. You can also change the label padding, which will control how those transforms behave (to give you more or less space). – tacaswell Jun 25 '13 at 22:34

0 Answers0