如何消除matplotlib中* images之间的间隙? [英] How to remove gaps between *images* in matplotlib?

查看:219
本文介绍了如何消除matplotlib中* images之间的间隙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

受到此问题的启发,我一直在试图绘制没有间隙的图像.

Inspired by this question, I've been trying to get images plotted without gaps.

在我的玩具示例中,我有四个要放置在两行中的图像.它们具有不同的形状:不同的行数,相同的列数.尽管存在差异,但它们应该适合于一个没有空隙的图形,如下图所示:

In my toy example I have four images that I want to place in two rows. They have different shapes: different number of rows, same number of columns. Despite the differences, they should fit in a single figure without gaps, as in the following illustration:

但是,当我尝试将它们放在一起时,设置plt.subplots_adjust(wspace=0, hspace=0)并不能解决问题,因为图像的形状不同.

When I try to get them together, though, setting plt.subplots_adjust(wspace=0, hspace=0) doesn't do the trick, because the images have different shapes.

代码如下:

from numpy.random import rand
import matplotlib.pyplot as plt

test_data = [[rand(10,10), rand(10,10)],[rand(5,10), rand(5,10)]]
f, axarr = plt.subplots(2,2)
for i in range(2):
    for j in range(2):
        axarr[i, j].imshow(test_data[i][j])
plt.tight_layout()
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()

我试过玩set_aspectequal,但是没有运气.

I've tried playing around with set_aspect and equal, but without luck.

有人知道如何消除这些差距吗?

Does anyone know how to get those gaps away?

推荐答案

您可以使用对plt.subplots的调用中的gridspec_kw参数设置子图的height_ratios,并使用不同图像的高度设置该比例.

You can set the height_ratios of the subplots using the gridspec_kw argument in the call to plt.subplots, and use the heights of the different images to set that ratio.

从文档到 plt.subplots() :

gridspec_kw:字典,可选

gridspec_kw : dict, optional

带有传递给用于创建子图所在网格的GridSpec构造函数的关键字的词段.

Dict with keywords passed to the GridSpec constructor used to create the grid the subplots are placed on.

请注意,您的示例具有与默认matplotlib图像大小完全相同的长宽比,因此,在添加更多行或更改图像形状之前,您不会注意到任何间隙.

Note that your example had the exact aspect ratio of the default matplotlib image size, so you wouldn't notice any gaps appearing until you add more rows, or change the shapes of the images.

因此,要将其扩展为常规解决方案,您将需要根据图像的形状设置图形大小.例如,让我们将示例扩展为3行2列.我们还将显式设置图形宽度为8英寸,并根据图像尺寸调整高度.

So, to expand this to a general solution, you will need to set the figure size according to the shapes of the images. For example, lets expand your example to 3 rows, 2 columns. We'll also explicitly set the figure width to 8 inches, and adjust the height based on the image sizes.

from numpy.random import rand
import matplotlib.pyplot as plt

test_data = [[rand(10,10), rand(10,10)],[rand(5,10), rand(5,10)],[rand(2,10), rand(2,10)]]
cmaps = [['viridis', 'binary'], ['plasma', 'coolwarm'], ['Greens', 'copper']]

heights = [a[0].shape[0] for a in test_data]
widths = [a.shape[1] for a in test_data[0]]

fig_width = 8.  # inches
fig_height = fig_width * sum(heights) / sum(widths)

f, axarr = plt.subplots(3,2, figsize=(fig_width, fig_height),
        gridspec_kw={'height_ratios':heights})

for i in range(3):
    for j in range(2):
        axarr[i, j].imshow(test_data[i][j], cmap=cmaps[i][j])
        axarr[i, j].axis('off')
plt.subplots_adjust(wspace=0, hspace=0, left=0, right=1, bottom=0, top=1)
plt.show()

这篇关于如何消除matplotlib中* images之间的间隙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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