简体   繁体   中英

Adjusting space in-between subplots

I am plotting 4 subplots in one figure, and I want to adjust the space in-between evenly.

I tried grid.GridSpec.update.

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

plt.figure(figsize=(8,8))
gs2 = gridspec.GridSpec(2, 2)
gs2.update(wspace=0.01, hspace=0.01)

ax1 = plt.subplot(gs2[0,0],aspect='equal')
ax1.imshow(img)
ax1.axis('off')

ax2 = plt.subplot(gs2[0,1],aspect='equal')
ax2.imshow(img)
ax2.axis('off')

ax3 = plt.subplot(gs2[1,0],aspect='equal')
ax3.imshow(img)
ax3.axis('off')

ax4 = plt.subplot(gs2[1,1],aspect='equal')
ax4.imshow(img)
ax4.axis('off')

The vertical space in-between 2 plots is too big, and it does not change no matter how I adjust gs2.update(hspace= ) , as shown below:

在此处输入图片说明

It's likely your aspect='equal' that's causing the problem.

Try this

import numpy as np
%matplotlib inline # if in a jupyter notebook like environment

img = np.ones((30, 30))

fig, axes = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(8,8),
                         gridspec_kw={'wspace': 0.01, 'hspace': 0.01})
axes = axes.ravel()

for ax in axes:
    # aspect : ['auto' | 'equal' | scalar], optional, default: None
    ax.imshow(img, aspect='auto')
    ax.axis('off')

在此处输入图片说明

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