问题描述
我想使用matplotlib在图像背景上绘制图形。我发现如何在matlab中进行操作
你可以看到我们有一架1600 x 1200的塞缪尔·杰克逊坦率地说,他乘坐的航空公司的航班对这条蛇非常恼火。
但是如果我们想在两个维度上绘制一条范围从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')
我不知道这条线是否会帮助杰克逊先生解决他的蛇问题。至少,它不会让事情变得更难。
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中绘制图像背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!