Adding a background image to a plot with known corner coordinates
Use the extent
keyword of imshow
. The order of the argument is [left, right, bottom, top]
import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import imread
import matplotlib.cbook as cbook
np.random.seed(0)
x = np.random.uniform(0.0,10.0,15)
y = np.random.uniform(0.0,10.0,15)
datafile = cbook.get_sample_data('lena.jpg')
img = imread(datafile)
plt.scatter(x,y,zorder=1)
plt.imshow(img, zorder=0, extent=[0.5, 8.0, 1.0, 7.0])
plt.show()
You must use the extent
keyword parameter:
imshow(img, zorder=0, extent=[left, right, bottom, top])
The elements of extent should be specified in data units so that the image can match the data. This can be used, for example, to overlay a geographical path (coordinate array) over a geo-referenced map image.