加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 删除matplotlib中图像行之间的垂直空白的问题

发布时间:2020-12-16 22:24:14 所属栏目:Python 来源:网络整理
导读:我正在显示带有pyplot的2D数组中的图像,并删除了轴标记和填充.但是,在图像行之间,仍然有我要删除的空白.图像本身没有空格. fig = plt.figure(figsize=(10,10))for x in range(quads_x): for y in range(quads_y): # ADD IMAGES fig.add_subplot(quads_y,quad

我正在显示带有pyplot的2D数组中的图像,并删除了轴标记和填充.但是,在图像行之间,仍然有我要删除的空白.图像本身没有空格.

fig = plt.figure(figsize=(10,10))

for x in range(quads_x):
    for y in range(quads_y):
        # ADD IMAGES
        fig.add_subplot(quads_y,quads_x,(quads_x * x) + y + 1)
        plt.imshow(cv2.imread("./map/" + winning_maps[x][y],0))

        # PYPLOT FORMATTING
        plt.subplots_adjust(wspace=0,hspace=0)
        ax = plt.gca()
        ax.axis("off")
        ax.xaxis.set_major_locator(matplotlib.ticker.NullLocator())
        ax.yaxis.set_major_locator(matplotlib.ticker.NullLocator())

代码产生类似的东西

关于我应该如何处理这个问题的任何想法?

最佳答案
通常使用plt.subplots_adjust(wspace = 0,hspace = 0)会将所有轴相互折叠.您遇到的问题是使用imshow修复了绘图中轴的纵横比.

要进行补偿,您需要调整画布的大小,使框架与您显示的图像具有相同的比例.下一个问题是轴周围的边界填充是图像大小的比例.如果你没问题,你可以删除边框,放入图像,然后将画布的高度调整为图形高度乘以图像的比例乘以图像的行数除以列的数量图片.

这是一个例子:

from matplotlib import pyplot as plt
from PIL import Image

img = Image.open('fox.jpg').resize(80,50)

fig,axes = plt.subplots(rows,columns,figsize=(7,7))
for ax in axes.ravel():
    ax.imshow(img)
    ax.set_autoscale_on(False)
    ax.axis('off')
plt.subplots_adjust(hspace=0,wspace=0,left=0,bottom=0,right=1,top=1)
r,c = axes.shape
fig.set_figheight(fig.get_figwidth() * ax.get_data_ratio() * r / c )
plt.show()

这是使用set_figheight之前的图像:

enter image description here

这是调整:

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读