23

I would like to position 5 subplots such that there are three of top and two at the bottom but next to each other. The current code gets close but I would like the final result to look like the following (ignore gray lines):

enter image description here

import matplotlib.pyplot as plt

ax1 = plt.subplot(231)
ax2 = plt.subplot(232)
ax3 = plt.subplot(233)
ax4 = plt.subplot(234)
ax5 = plt.subplot(236)

plt.show()

Current rendering

Rohit
  • 5,840
  • 13
  • 42
  • 65

2 Answers2

25

You can use colspan When you use suplot2grid instead of subplot.

import matplotlib.pyplot as plt

ax1 = plt.subplot2grid(shape=(2,6), loc=(0,0), colspan=2)
ax2 = plt.subplot2grid((2,6), (0,2), colspan=2)
ax3 = plt.subplot2grid((2,6), (0,4), colspan=2)
ax4 = plt.subplot2grid((2,6), (1,1), colspan=2)
ax5 = plt.subplot2grid((2,6), (1,3), colspan=2)

And then every subplot needs to be 2 cols wide, so that the subplots in the second row can be shifted by 1 column.

espang
  • 673
  • 6
  • 6
  • To adjust the horizontal and vertical spacing you can use `plt.subplots_adjust(hspace=0.5, wspace=15)` or modified values. – CodeSocke Aug 21 '22 at 13:20
13

You can also try to make a 2x3 subplot, make the last one invisible and set coordinates of the 4th and 5th plots like this:

fig, axes = plt.subplots(2,3, figsize=(15,10))
axes[1][2].set_visible(False)

axes[1][0].set_position([0.24,0.125,0.228,0.343])
axes[1][1].set_position([0.55,0.125,0.228,0.343])

This is a quick and dirty process that can be manipulated flexibly.