简体   繁体   中英

reducing spacing in subplots of matplotlib

import matplotlib.pyplot as plt
import numpy as np
f, axarr = plt.subplots(nrows=10, ncols=10,
                                gridspec_kw={'hspace': 0, 'wspace': 0})
for dim in range(10):
    samples = np.random.rand(10, 256, 256, 1)
    for sample_idx in range(10):
        axarr[dim][sample_idx].imshow(samples[sample_idx,:,:,0],
                                             cmap='gray')
        axarr[dim][sample_idx].set_axis_off()
plt.show()

在此处输入图像描述

My code above gives the plot below for some reason. I'm trying to make it so that each of the subplots are right next to each other, with maybe.01 of spacing in between. Right now, for some reason, the columns are too spaced apart; it's not letting me change their spacing. Anyone have any suggestions? Thanks.

Usually I would use f.subplots_adjust(wspace=desired_hor_spacing) but in your case I think the plots are already as close as they can be without leaving any "empty space" on the left and right of the plot.

You can change the left and right margin of the plot to get around this:

f.subplots_adjust(left=.1, right=.9)

Which gives you: 在此处输入图像描述

If you make the left margin larger and the right one smaller you can get them to be even tighter.

You can even change the left and right parameters interactively by clicking on the "configure subplots" button when you show the plot.

To get around the empty space on the left and right you can either crop the image or change the aspect ratio of the figure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM