在 python 中绘制图像背景 [英] Plot over an image background in python

查看:50
本文介绍了在 python 中绘制图像背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 matplotlib 在图像背景上绘制图形.我找到了如何在 matlab

你可以看到我们有一张 1600 x 1200 的塞缪尔·杰克逊,坦率地说,他对飞机上的蛇很生气.

但是如果我们想在这两个维度上绘制一条从 0 到 300 的线,我们可以这样做:

fig, ax = plt.subplots()x = 范围(300)ax.imshow(img, extent=[0, 400, 0, 300])ax.plot(x, x, '--', linewidth=5, color='firebrick')

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

I would like to plot a graph over an image background using matplotlib. I found how to do it in matlab http://www.peteryu.ca/tutorials/matlab/plot_over_image_background

I've tried something basic like this:

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

but it gave me something really ugly:

解决方案

Just like in the MATLAB example that you linked to, you have to specify the desired extent of the image when you call in imshow.

By default, matplotlib and MATLAB both place the upper left corner of the image the origin, go down and to the right from there, and set each pixel as a 1x1 square in coordinate space. This is what your image is doing.

You can control this with the extent parameter which takes the form of a list [left, right, bottom, top].

Not using extent looks like this:

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

You can see that we have a 1600 x 1200 of Samuel L. Jackson getting, quite frankly, rather annoyed with the snake aboard his airline flight.

But if we want to plot a line ranging from 0 to 300 in both dimension over this, we can do just that:

fig, ax = plt.subplots()
x = range(300)
ax.imshow(img, extent=[0, 400, 0, 300])
ax.plot(x, x, '--', linewidth=5, color='firebrick')

I don't know if the line will help Mr. Jackson with his snake problem. At the very least, it won't make things any harder.

这篇关于在 python 中绘制图像背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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