我需要将此代码从matlab移植到python:
fig;
stringNumber = '0.1'
set(gca,'units','pixels','position',[1 1 145 55],'visible','on')
box('on')
上面的代码导致下图matlabTest1(屏幕最大化)。
请注意,如果调整大小,轴将不会缩放,请参见matlabTest2
我尝试在python中移植它,将其位置和偏移量从transFigure转换为Display / Pixel。
这是我的代码:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca()
inv = fig.transFigure.inverted()
offset = inv.transform((1, 1))
position = inv.transform((145, 55))
ax.set_position([offset[0], offset[1], position[0],position[1]])
plt.show()
我的代码导致pythonTest1(屏幕最大化)。框的大小看起来与matlabTest1不同。
另外,如果我调整图的大小,则框的大小也会更改,请参见pythonTest2
如何获得与Matlab代码完全相同的结果?
感谢任何可以帮助您的人。
最佳答案
Matplotlib图形默认情况下位于图形坐标中。因此,没有直接等价于提供的matlab代码。
指定像素位置的轴的一种方法是使用AnchoredSizeLocator
中的mpl_toolkits.axes_grid1.inset_locator
。
import matplotlib.transforms as mtrans
import mpl_toolkits.axes_grid1.inset_locator as ins
import matplotlib.pyplot as plt
axes_locator = ins.AnchoredSizeLocator([1, 1, 145, 55],
"100%", "100%",
loc="center",
bbox_transform=mtrans.IdentityTransform(),
borderpad=0)
fig, ax = plt.subplots()
ax.set_axes_locator(axes_locator)
plt.show()