image - 在 python 中绘制图像背景

标签 image matplotlib background

这个问题在这里已经有了答案:





Adding a background image to a plot with known corner coordinates

(2 个回答)


3年前关闭。




我想使用 matplotlib 在图像背景上绘制图形。我在 matlab 中找到了如何做 http://www.peteryu.ca/tutorials/matlab/plot_over_image_background

我已经尝试了一些基本的东西:

im = plt.imread("dd.png")
implot = plt.imshow(im)
theta=np.linspace(0,2*np.pi,50)
z=np.cos(theta)*39+145
t=np.sin(theta)*39+535-78+39
plt.plot(z,t)
plt.show()

但它给了我一些非常难看的东西:

something really ugly

最佳答案

就像在您链接到的 MATLAB 示例中一样,您必须在调用 imshow 时指定所需的图像范围。 .

默认情况下,matplotlib 和 MATLAB 都将图像的左上角置于原点,从那里向下和向右,并将每个像素设置为坐标空间中的 1x1 正方形。这就是你的形象正在做的事情。

您可以使用 extent 来控制它。采用列表形式的参数 [left, right, bottom, top] .

不使用范围看起来像这样:

import matplotlib.pyplot as plt
img = plt.imread("airlines.jpg")
fig, ax = plt.subplots()
ax.imshow(img)

enter image description here

你可以看到我们有一张 1600 x 1200 的 Samuel L. Jackson,坦率地说,他对飞机上的蛇很恼火。

但是如果我们想在这两个维度上绘制一条从 0 到 300 的线,我们可以这样做:
fig, ax = plt.subplots()
x = range(300)
ax.imshow(img, extent=[0, 400, 0, 300])
ax.plot(x, x, '--', linewidth=5, color='firebrick')

enter image description here

我不知道这条线是否能帮助 jackson 先生解决他的蛇问题。至少,它不会让事情变得更难。

关于image - 在 python 中绘制图像背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34458251/

相关文章:

javascript - 使用链接的类更改背景

css - 如何使用 index.css 在 google app engine 中为留言簿应用程序插入背景图片?

javascript - 为什么我的灯箱画廊会在单独的页面中加载 img?

reactjs - 如何使用 React 检查文件扩展名

python - 直方图上的非归一化高斯曲线

python - Jupyter 笔记本中未显示的绘图

python - Pandas 数据帧,ValueError : shape mismatch: objects cannot be broadcast to a single shape

ruby-on-rails - 如何在 Rake 任务中在后台运行函数?

c++ - 在没有其他库的情况下用纯 c/c++ 编写 BMP 图像

java - 如何用java创建图像编辑器?