Matplotlib:坐标变换

Matplotlib: Transformation of coordinates

我已经为这个问题苦苦挣扎了一段时间。 我只是想在 display world 中转换我的数据,执行一些操作,然后转换回世界坐标。 听起来很简单,根据文档应该做如下:

fig, ax = plt.subplots(figsize=(5, 5))
ax.plot([1,2], [1,2], "or")  
ax.set_xlim((0, 5))
ax.set_ylim((0, 5))
ax.set_axis_off()

p1 = [0, 0]
p1x, p1y = ax.transData.transform(p1)
print(f"p1x={p1x:.3f}, p1y={p1y:.3f}")

这导致 p1x=125.000, p1y=110.000

那是因为你的情节周围有空白。您正在打印的值是 window.

中原点的坐标

您可以使用 plt.subplots_adjust(left=0, right=1, top=1, bottom=0) 删除边距:

fig, ax = plt.subplots(figsize=(5, 5))
ax.plot([1,2], [1,2], "or")  
ax.set_xlim((0, 5))
ax.set_ylim((0, 5))
ax.set_axis_off()

plt.subplots_adjust(left=0, right=1, top=1, bottom=0)

p1 = [0, 0]
p1x, p1y = ax.transData.transform(p1)
print(f"p1x={p1x:.3f}, p1y={p1y:.3f}")

输出:

p1x=0.000, p1y=0.000