正如问题标题中所写,我希望能够在图形上放置图像,图像大小以英寸为单位。
答案 0 :(得分:1)
也许figimage会对你有用吗?您只需考虑数字dpi并相应地重塑您的图像。我写了一个简短的放置例程,应该使用RectBiVariateSpline
进行插值:
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import RectBivariateSpline
def place_img_on_fig(fig, img, size_inches):
# Figure out the right size
dpi = fig.dpi
new_pxsize = [i * dpi for i in size_inches]
# Interpolate to the right size
spline = RectBivariateSpline(np.arange(img.shape[0]),
np.arange(img.shape[1]),
img)
newx = np.arange(new_pxsize[0]) * (np.float(img.shape[0]) / new_pxsize[0])
newy = np.arange(new_pxsize[1]) * (np.float(img.shape[1]) / new_pxsize[1])
new_img = spline(newx, newy)
# Call to figimage
fig.figimage(new_img, cmap='jet')
# Make the figure
fig = plt.figure(figsize=(5,1), dpi=plt.rcParams['savefig.dpi'])
# Make a test image (100,100)
xx, yy = np.mgrid[0:100,0:100]
img = xx * yy
# Place the image
place_img_on_fig(fig, img, (0.5,4))
plt.show()
这样可以将100x100图像放在图中,就像它高0.5英寸,宽4英寸一样。如果您愿意,可以向figimage
电话添加抵消。