Search code examples
pythonmatplotlibjupytersubplot

How do I get the longer plot on top using subplots?


This is a simple question, based on this previously asked subplots question: Matplotlib different size subplots

But how do I get the larger plot on top? I know that in plt.subplots(), it's nrows and ncolumns, but I am having trouble placing the larger plot on top and the 3 smaller ones below:

plt.figure(figsize=(12, 6))

ax1 = plt.subplot(2,1,2)
ax2 = plt.subplot(2,3,1)
ax3 = plt.subplot(2,3,2)
ax4 = plt.subplot(2,3,3)

axes = [ax1, ax2, ax3, ax4]

enter image description here

Hoping to get the the bottom plot above the top 3. Thank you!


Solution

  • You need to specify a 2 x 3 plot layout and make the first plot span all three columns.

    See subplot:

    index can also be a two-tuple specifying the (first, last) indices (1-based, and including last) of the subplot, e.g., fig.add_subplot(3, 1, (1, 2)) makes a subplot that spans the upper 2/3 of the figure.

    plt.figure(figsize=(12, 6))
    
    ax1 = plt.subplot(2,3,(1,3))
    ax2 = plt.subplot(2,3,4)
    ax3 = plt.subplot(2,3,5)
    ax4 = plt.subplot(2,3,6)
    

    enter image description here