在图像顶部绘图时,删除matplotlib中的空格 [英] Remove whitespace in matplotlib when plotting on top of image

查看:30
本文介绍了在图像顶部绘图时,删除matplotlib中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多方法,但似乎没有一个起作用.我到处看的人都说设置 bbox_inches ='tight',但这似乎行不通.

基本上我的目标是在像素坐标中在图像顶部绘制一些点,然后保存它,边框周围没有任何空白.我的代码当前如下所示:

 将matplotlib.pyplot导入为pltplt.ion()img = plt.imread('myimage.jpg')ymax,xmax,_ = img.shapeplt.imshow(img,scope = [0,xmax,ymax,0])#切换到像素坐标plt.plot([100,200,300],[100,200,300],'ro')plt.imshow(img, extent=[0,xmax,ymax,0]) # 中心图plt.axis('关闭')plt.savefig('out.jpg', bbox_inches='tight', pad_inches=0)

但是我保存的图像周围仍然有白色边框.我该如何解决?

解决方案

似乎 bbox_inches ='tight'选项试图使您的图形适应轴刻度.为了避免这种情况,您可以将图形的实际范围提供给 plt.savefig 方法.

 将matplotlib.pyplot导入为pltimg = plt.imread('myimage.jpg')ymax,xmax,_ = img.shapeplt.imshow(img, extent=[0,xmax,ymax,0]) # 切换到像素坐标plt.plot([100,200,300],[100,200,300],'ro')plt.imshow(img, extent=[0,xmax,ymax,0]) # 中心图plt.axis('关闭')# 获取图形和轴句柄无花果 = plt.gcf()ax = plt.gca()# 获取轴范围范围 = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())plt.savefig('out.jpg', bbox_inches=extent)

如果出于某种原因,您想为您的图形添加一个小的白色边缘,您仍然可以这样做;只需按照以下说明扩展范围

extent2 = extent.expanded(1.01, 1.01)plt.savefig('out.jpg',bbox_inches = extent2)

另一个选择是使用

I have tried many methods, but none seem to work. Everywhere I look people say set bbox_inches='tight' but that doesn't seem to work.

Basically my goal is to plot some points on top of the image in pixel coordinates and then save it without the any whitespace around the border. My code currently looks like this:

import matplotlib.pyplot as plt
plt.ion()
img = plt.imread('myimage.jpg')
ymax, xmax, _ = img.shape
plt.imshow(img, extent=[0,xmax,ymax,0]) # switch to pixel coords
plt.plot([100,200,300],[100,200,300],'ro')
plt.imshow(img, extent=[0,xmax,ymax,0]) # recenter plot
plt.axis('off')
plt.savefig('out.jpg', bbox_inches='tight', pad_inches=0)

But my saved image still have a white border around it. How can I fix this?

解决方案

It seems the bbox_inches='tight' option tries to fit your figure with axis ticks in mind. In order to avoid that you can provide the the actual extent of the figure to plt.savefig method.

import matplotlib.pyplot as plt
img = plt.imread('myimage.jpg')

ymax, xmax, _ = img.shape
plt.imshow(img, extent=[0,xmax,ymax,0]) # switch to pixel coords
plt.plot([100,200,300],[100,200,300],'ro')
plt.imshow(img, extent=[0,xmax,ymax,0]) # recenter plot
plt.axis('off')

# get figure and axis handle 
fig = plt.gcf()
ax = plt.gca()
# get axis extent
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
plt.savefig('out.jpg', bbox_inches=extent)

If, for some reason, you want to add a small white edge to your figure, you can still do that; just expand the extent as follow

extent2 = extent.expanded(1.01, 1.01)
plt.savefig('out.jpg', bbox_inches=extent2)

An other option will be to get-rid off the axis boundary completely using matehat's suggestion.

import matplotlib.pyplot as plt


img = plt.imread('myimage.jpg')
ymax, xmax, _ = img.shape
my_dpi = 80.
my_figsize = [xmax/my_dpi, ymax/my_dpi]
fig  = plt.figure(figsize=my_figsize, dpi=my_dpi)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)

h_im = ax.imshow(img, aspect = 'equal', extent=[0,xmax,ymax,0])
plt.plot([100,200,300],[100,200,300],'ro')
plt.axis(h_im.get_extent())
plt.savefig('out.jpg', dpi = my_dpi)

Although the final image has the original image size there is a sub-pixle difference between the two images. The figure below shows the difference between the myimage.jpg and out.jpg.

这篇关于在图像顶部绘图时,删除matplotlib中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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