How to set different spaces in a subplotlib?

Hi, could you help me with this, please? I have this script for plotting subplot. How to set the space between the first and the second rows of plots? I hid an axis and the limits are set in such a way that the line is in some distance from margins. Thus, there is a bigger space with comparison to other plots. How to set that space smaller? I tried plt.tight_layout(), but it was not successful. It set all spaces. Thank you

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D                 # 3d graph
from mpl_toolkits.mplot3d import proj3d                 # 3d graph

# Plot subplot 
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 2, figsize=(10,13))

x = [-1, 0]
y = [-5, 5]
fig1 = plt.subplot(421)
plt.plot(x,y)
fig1.set_ylim(-8.3,8.3)
fig1.set_xlim(-8.3,8.3)
fig1.set_aspect(np.diff(fig1.get_xlim())/np.diff(fig1.get_ylim()))
plt.axis('off')

fig2 = plt.subplot(422)
plt.plot(x,y)
plt.plot(x,y)
fig2.set_ylim(-8.3,8.3)
fig2.set_xlim(-8.3,8.3)
fig2.set_aspect(np.diff(fig2.get_xlim())/np.diff(fig2.get_ylim()))
plt.axis('off')

fig3 = plt.subplot(423, projection = '3d') 
fig4=plt.subplot(424, projection = '3d')
fig5=plt.subplot(425, projection = '3d')
fig6=plt.subplot(426, projection = '3d')
fig7=plt.subplot(427, projection = '3d')
fig8=plt.subplot(428, projection = '3d')
plt.show()

The automatic layout tools don’t have that fine control, but given an individual Axes object (your fig1, fig2…) you can set the postion explicitly ( https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.axes.Axes.set_position.html ).

You can also use GridSpec to layout your 2D and 3D sub-axes separately (see https://matplotlib.org/3.2.1/tutorials/intermediate/gridspec.html). The GridSpec using SubplotSpec section shows how to use nested GridSpec to layout two sets of Axes

fig = plt.figure()
gs0 = fig.add_gridspec(2, 1, height_ratios=(1, 3))

gs_top = gs0[0].subgridspec(1, 2)
gs_bottom = gs0[1].subgridspec(3, 2)

for a in range(2):
    fig.add_subplot(gs_top[0, a])    
    for b in range(3):
        fig.add_subplot(gs_bottom[b, a], projection='3d')


as a starting point and then some tweaking to the spacing / layout settings of the grid specs.

1 Like

Thank you a lot for your answer. I helped.