Home > Net >  Adjust individual subplot spacing
Adjust individual subplot spacing

Time:09-30

fig = plt.figure(figsize=(14, 14))
ax0 = fig.add_subplot(12, 2, (1, 9))
ax1 = fig.add_subplot(12, 2, (2, 10))
ax2 = fig.add_subplot(14, 2, (13, 14))
ax3 = fig.add_subplot(14, 2, (15, 16))

for ax in (ax0, ax1, ax2, ax3):
    ax.set_xticks([])
    ax.set_yticks([])
fig.subplots_adjust(hspace=.25, wspace=.02)

yields

Can I reduce vertical spacing between bottom two subplots (ax2, ax3) only?

CodePudding user response:

This is a bit hacky, but you could play with the bounding boxes of each subplot to move that of ax3 closer to ax2 (here touching):

fig = plt.figure(figsize=(5, 5))        # made smaller for example
ax0 = fig.add_subplot(12, 2, (1, 9))
ax1 = fig.add_subplot(12, 2, (2, 10))
ax2 = fig.add_subplot(14, 2, (13, 14))
ax3 = fig.add_subplot(14, 2, (15, 16))

for ax in (ax0, ax1, ax2, ax3):
    ax.set_xticks([])
    ax.set_yticks([])

fig.subplots_adjust(hspace=.25, wspace=.02)

# below is where the m"hack"gic happens
pos_ax3 = ax3.get_position()
pos_ax2 = ax2.get_position()
ywidth = pos_ax3.y1 - pos_ax3.y0

yspace = 0
pos_ax3.y0 = pos_ax2.y0 - ywidth * yspace
pos_ax3.y1 = pos_ax2.y0 - ywidth * (1   yspace)
ax3.set_position(pos_ax3)

output:

manually shifted axes

  • Related