Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

python - How to remove gaps between *images* in matplotlib?

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:

it fits

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.

Here's the code:

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

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

nofits

Does anyone know how to get those gaps away?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

From the docs to plt.subplots():

gridspec_kw : dict, optional

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

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.

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...