在matplotlib中以物理坐标定位轴

在matplotlib中以物理坐标定位轴,matplotlib,Matplotlib,可以使用以下命令在matplotlib中的地物坐标中设置轴的位置 ax.set_position([left, bottom, width, height]) 有没有办法用物理坐标而不是数字单位来设置?在绘图时,我可以通过将变换设置为fig.dpi_scale_trans来实现这一点,但我不知道如何在这里实现这一点。当然,我可以获得图形坐标,然后计算相应的坐标,但我希望它在图形大小更改时更新。为了以绝对单位(例如英寸)设置轴位置,您可以使用toolkits模块中的AnchoredSizeLoc

可以使用以下命令在matplotlib中的地物坐标中设置轴的位置

ax.set_position([left, bottom, width, height])

有没有办法用物理坐标而不是数字单位来设置?在绘图时,我可以通过将变换设置为fig.dpi_scale_trans来实现这一点,但我不知道如何在这里实现这一点。当然,我可以获得图形坐标,然后计算相应的坐标,但我希望它在图形大小更改时更新。

为了以绝对单位(例如英寸)设置轴位置,您可以使用toolkits模块中的
AnchoredSizeLocator
覆盖轴定位器。 例如,要使轴距离左下角1英寸,宽度和高度为3英寸:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import AnchoredSizeLocator

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])

# Coordinates in inches, left, bottom, width, height
coords_inches = [1,1,3,3]
axes_locator = AnchoredSizeLocator(coords_inches, "100%", "100%",
                                   loc="center",
                                   bbox_transform= fig.dpi_scale_trans,
                                   borderpad=0)

ax.set_axes_locator(axes_locator)

plt.show()