gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:18:14 26 4
gpt4 key购买 nike

我正在使用 pyplot 显示二维数组中的图像,并删除了轴标记和填充。但是,在图像行之间,仍然有我想删除的空白。图片本身没有空格。

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

代码产生类似的东西

this

关于我应该如何处理这个问题有什么想法吗?

最佳答案

通常使用 plt.subplots_adjust(wspace=0, hspace=0) 会将所有轴折叠到彼此上。您遇到的问题是使用 imshow 修复了图中轴的纵横比。

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

这是一个例子:

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

关于python - 删除 matplotlib 中图像行之间的垂直空白的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53900578/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com