python画图像_在python中绘制图像背景

本文介绍如何使用Matplotlib在图片背景上创建专业图表,重点讲解了imshow函数的extent参数,通过实例演示了如何调整坐标范围以使线条与背景图完美融合,避免了原始代码导致的视觉效果不佳问题。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

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:

iZ7MM.png

解决方案

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)

UIU28.png

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

0FPTg.png

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值