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.
Asked
Active
Viewed 117 times
0
-
Possible duplicate of [How do you change the size of figures drawn with matplotlib?](http://stackoverflow.com/questions/332289/how-do-you-change-the-size-of-figures-drawn-with-matplotlib) – Filip Roséen - refp Oct 10 '15 at 16:34
-
There are answers in that thread that directly address your need (and explain what you need to know). – Filip Roséen - refp Oct 10 '15 at 16:44
-
No, why do say that? The question here is about adding an image to a matplotlib figure, not about the specific size of the figure itself. If you have took one second to look at my profile you see that i am not a total newcomer to matplotlib, while you don't seem to use it at all, so why discussing specifics with me? – tillsten Oct 10 '15 at 17:50
-
So this gets now downvoted because people who don`t know matplotlib think the question is unclear *sight*? – tillsten Oct 12 '15 at 11:27
-
my recommendation would be to put some example code (no matter how trivial it might be); this usually indicates some effort, and are less prone to receive downvotes. And just for the record; the question currently have *one* downvote (no upvotes). – Filip Roséen - refp Oct 12 '15 at 11:51
-
Does figimage not work for you? You'd have to resize the image according to the appropriate figure dpi manually first, but I'd think that would work.... – Ajean Oct 13 '15 at 00:53
1 Answers
1
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.

Ajean
- 5,528
- 14
- 46
- 69