1

我注意到,当在轴标签(例如)上使用Text.set_xText.set_y或或时,标签会忽略除主轴以外的维度 - 即,如果返回 (0.5, 325),如果我运行,则只有 x 组件更新,给出一个位置(0.25, 325)。Text.set_position()gca().xaxis.labelxaxis.get_position()xaxis.set_position(0.25, 400)

我准备了一个说明图: X 轴运动

使用下面的 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()
4

0 回答 0