[英]Port Matlab code involving matplotlib setting axes position in pixels
我需要將此代碼從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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.