我注意到在轴标签上使用Text.set_x
或Text.set_y
或Text.set_position()
时(例如gca().xaxis.label
),标签会忽略除主轴以外的尺寸 - 即如果xaxis.get_position()
返回(0.5,325),如果我运行xaxis.set_position(0.25, 400)
,则只有x组件更新,给出的位置为(0.25,325)。
我准备了一个说明性的情节:
使用以下MWE生成。如您所见,标签只想向一个方向移动。显然我可以使用labelpad来移动它们,但是有不对称性似乎很奇怪,我有兴趣知道它为什么存在,如果还有另一种方法我错过了将轴位置设置为任意坐标?
MWE代码如下:
# 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()