1

I have the following code:

fig = plt.figure(figsize = (16,9))   
ax1 = plt.subplot(3, 2, 1)
ax2 = plt.subplot(3, 2, 2)
ax3 = plt.subplot(3, 2, 3)
ax4 = plt.subplot(3, 2, 4)
ax5 = plt.subplot(3, 1, 3)

enter image description here

However, I would like the third row (the plot that spans two columns) to be reduced in height by half. How do I do this?

I am aware of this post: Matplotlib different size subplots but don't understand how I can change the height of the lowest plot that spans two columns.

To prevent confusion: I want to cut the lower plot in half (delete the part in red) and therefore make it smaller (height-wise).

enter image description here

JonnDough
  • 827
  • 6
  • 25

1 Answers1

3

The post you link to has the answers that will help you but the simplest way nowadays (since matplotlib-3.3.0) is probably to use plt.subplot_mosaic and pass the height ratios to the gridspec_kw argument.

e.g.

fig, axes = plt.subplot_mosaic("AB;CD;EE", gridspec_kw=dict(height_ratios=[1, 1, 0.5]))

which will give you

enter image description here

This returns axes as a dictionary of Axes with the name you give it, so you can plot with e.g.

axes["A"].plot([1, 2, 3], [1, 2, 3])

(you could also "double stack" your mosaic like so for the same effect without using gridspec_kw)

fig, axes = plt.subplot_mosaic("AB;AB;CD;CD;EE")

If for some reason you don't want to (or can't) use suplot_mosaic then you can do this with GridSpec directly (example here).

from matplotlib.gridspec import GridSpec

fig = plt.figure()
gs = GridSpec(nrows=3, ncols=2, figure=fig, height_ratios=[1, 1, 0.5])
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[1, 0])
ax4 = fig.add_subplot(gs[1, 1])
ax5 = fig.add_subplot(gs[2, :])

which will give you the same thing.

tomjn
  • 5,100
  • 1
  • 9
  • 24