Reputation: 14878
As written in the question title, I want to be able to place an image on the figure with the images size given in inches.
Upvotes: 0
Views: 123
Reputation: 5659
Perhaps figimage will work for you? You just have to account for the figure dpi and reshape your image accordingly. I wrote a short placement routine that should size it right, using a RectBiVariateSpline
for the interpolation:
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()
This places the 100x100 image on the figure like it is 0.5 inches high and 4 inches wide. You can add offsets to the figimage
call if you so desire.
Upvotes: 1