将背景图像添加到具有已知角坐标的绘图中 [英] Adding a background image to a plot with known corner coordinates

查看:42
本文介绍了将背景图像添加到具有已知角坐标的绘图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我正在以图像为背景绘制一组点.我在示例中使用了 Lena 图片:

Say I am plotting a set of points with an image as a background. I've used the Lena image in the example:

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()

这给了我.

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

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.0 and the top-right corner to be at x, y = 8.0, 7.0.

推荐答案

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

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()

这篇关于将背景图像添加到具有已知角坐标的绘图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆