pythonmatplotlibsubplotmatplotlib-gridspec

Remove empty spacing between gridspec subplots


I am trying to create a plot in matplotlib with three subplots in the first column and two in the second.

Using gridspec, I have managed to tweak it, but somehow there are large spacing between the different subplots in the first and second column. Ideally, they should fill the whole subplot area. Any advice or explanation why this is happening?

Thanks in advance!

What I tried so far:

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

fig = plt.figure(figsize=(7, 7.5))
gs = gridspec.GridSpec(6, 2)

# Top left subplot
ax = fig.add_subplot(gs[0:1, 0])
ax.set_ylabel('YLabel0')
ax.set_xlabel('XLabel0')
# Center left subplot
ax = fig.add_subplot(gs[2:3, 0])
ax.set_ylabel('YLabel1')
ax.set_xlabel('XLabel1')
# Bottom left subplot
ax = fig.add_subplot(gs[4:5, 0])
ax.set_ylabel('YLabel2')
ax.set_xlabel('XLabel2')
# Top right subplot
ax = fig.add_subplot(gs[0:2, 1])
ax.set_ylabel('YLabel3')
ax.set_xlabel('XLabel3')
# Bottom right subplot
ax = fig.add_subplot(gs[3:5, 1])
ax.set_ylabel('YLabel4')
ax.set_xlabel('XLabel4')

plt.show()

And this is the result:

enter image description here


Solution

  • You can create two GridSpec instances, one for the left column, and one for the right. For example:

    import matplotlib.pyplot as plt
    fig = plt.figure(figsize=(7, 7.5))
    
    # Left column has 3 rows
    gs1 = plt.GridSpec(3, 2)
    
    # Right column has 2 rows
    gs2 = plt.GridSpec(2, 2)
    
    # Create left column axes using gs1
    ax11 = fig.add_subplot(gs1[0, 0])
    ax12 = fig.add_subplot(gs1[1, 0])
    ax13 = fig.add_subplot(gs1[2, 0])
    
    # Create right column axes using gs2
    ax21 = fig.add_subplot(gs2[0, 1])
    ax22 = fig.add_subplot(gs2[1, 1])
    
    plt.show()
    

    enter image description here