将背景图像添加到绘图

2024-09-27 04:25:06 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我正在以图像为背景绘制一组点。我在示例中使用了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()

这给了我enter image description here

我的问题是:如何在绘图中指定图像的角坐标?假设我希望左下角在x, y = 0.5, 1.0,右上角在x, y = 8.0, 7.0


Tags: 图像importnumpy示例imgasnp绘制
2条回答

使用imshowextent关键字。参数的顺序是[left, right, bottom, top]

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
x = np.random.uniform(0.0,10.0,15)
y = np.random.uniform(0.0,10.0,15)

datafile = 'lena.jpg'
img = plt.imread(datafile)
plt.scatter(x,y,zorder=1)
plt.imshow(img, zorder=0, extent=[0.5, 8.0, 1.0, 7.0])
plt.show()

enter image description here

  • 对于希望在散点图的小区域中具有图像的情况,更改图的顺序(.imshow然后.scatter)并更改extent
plt.imshow(img, zorder=0, extent=[3.0, 5.0, 3.0, 4.50])
plt.scatter(x, y, zorder=1)
plt.show()

enter image description here

必须使用extent关键字参数:

imshow(img, zorder=0, extent=[left, right, bottom, top])

区段的元素应以数据单位指定,以便图像可以与数据匹配。例如,这可用于将地理路径(坐标阵列)覆盖在地理参考地图图像上

相关问题 更多 >

    热门问题