Python 将背景图像添加到具有已知角坐标的绘图中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15160123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a background image to a plot with known corner coordinates
提问by YXD
Say I am plotting a set of points with an image as a background. I've used the Lenaimage in the example:
假设我正在以图像为背景绘制一组点。我在示例中使用了Lena图像:
import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import imread
np.random.seed(0)
x = np.random.uniform(0.0,10.0,15)
y = np.random.uniform(0.0,10.0,15)
img = imread("lena.jpg")
plt.scatter(x,y,zorder=1)
plt.imshow(img,zorder=0)
plt.show()
This gives me
.
这给了我
。
My question is: How can I specify the corner coordinates of the image in the plot? Let's say I'd like the bottom-left corner to be at x, y = 0.5, 1.0and the top-right corner to be at x, y = 8.0, 7.0.
我的问题是:如何在图中指定图像的角坐标?假设我希望左下角位于x, y = 0.5, 1.0,右上角位于x, y = 8.0, 7.0。
采纳答案by David Zwicker
Use the extentkeyword of imshow. The order of the argument is [left, right, bottom, top]
使用extent关键字imshow. 论证的顺序是[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()
回答by heltonbiker
You must use the extentkeyword parameter:
您必须使用extent关键字参数:
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.
范围的元素应以数据单位指定,以便图像可以匹配数据。例如,这可用于在地理参考地图图像上覆盖地理路径(坐标数组)。

