Python - different size subplots in matplotlib

20,472

Solution 1

you should have a look at the GridSpec documentation for more flexibility when creating axes

ax1 = plt.subplot2grid((2,3),(0,0))
ax2 = plt.subplot2grid((2,3),(0,1))
ax3 = plt.subplot2grid((2,3),(0,2))
ax4 = plt.subplot2grid((2,3),(1,0), colspan=3)

Solution 2

Use gridspec:

from matplotlib.gridspec import GridSpec
import matplotlib.pyplot as plt

fig=plt.figure()

gs=GridSpec(2,3) # 2 rows, 3 columns

ax1=fig.add_subplot(gs[0,0]) # First row, first column
ax2=fig.add_subplot(gs[0,1]) # First row, second column
ax3=fig.add_subplot(gs[0,2]) # First row, third column
ax4=fig.add_subplot(gs[1,:]) # Second row, span all columns

fig.savefig('gridspec.png')

enter image description here

Solution 3

import pylab as pl
ax1 = pl.subplot(2, 3, 1)
ax2 = pl.subplot(2, 3, 2)
ax3 = pl.subplot(2, 3, 3)
ax4 = pl.subplot(2, 1, 2)
Share:
20,472
user3601754
Author by

user3601754

Updated on July 17, 2022

Comments

  • user3601754
    user3601754 almost 2 years

    I have 4 subplots, i achieve to plot 3 subplots on 1 line but i can t add an other plot on a second line which must have the same width of the 3 figures. The first 3 figures : enter image description here

    The second figure, i want to add:

    enter image description here

    I try that :

    plt.close()
    
    font={'family':'sans-serif','weight':'normal','size':'14'}
    plt.rc('font',**font)
    
    fig, axes = plt.subplots(nrows=3, ncols=1)
    
    
    plt.tight_layout(pad=0.05, w_pad=0.001, h_pad=2.0)
    ax1 = plt.subplot(231) # creates first axis
    
    #ax1.text(20, 2500, 'CYCLE :', size=14) 
    
    #ax1.text(1000, 2500, str((image)*50+50), size=14) 
    
    ax1.tick_params(labelsize=8) 
    ax1.set_xticks([0,2000,500,1000,1500])
    ax1.set_yticks([0,2000,500,1000,1500])
    ax1.tick_params(labelsize=8) 
    ax1.imshow(mouchetis[::-1,::],alpha=0.6)
    ax1.imshow(V_exp_invmaskA,cmap='hot',extent=(X.min(),2015,Y.min(),2015), alpha=0.4)
    i1 = ax1.imshow(V_maskA,cmap='hot',extent=(X.min(),2015,Y.min(),2015),alpha=0.8)
    ax1.set_xticklabels([0,2000,500,1000,1500])
    ax1.set_yticklabels([0,2000,500,1000,1500])
    ax1.set_title("MASQUE A", y=1.05, fontsize=10)
    
    
    ax2 = plt.subplot(232) # creates second axis
    ax2.set_xticks([0,2000,500,1000,1500])
    ax2.set_yticks([0,2000,500,1000,1500])
    ax2.imshow(mouchetis[::-1,::],alpha=0.6)
    ax2.imshow(V_exp_invmaskB,cmap='hot',extent=(X.min(),2015,Y.min(),2015), alpha=0.4)
    i2 = ax2.imshow(V_maskB,cmap='hot',extent=(X.min(),2015,Y.min(),2015),alpha=0.8)
    ax2.set_title("MASQUE B", y=1.05, fontsize=10)
    ax2.set_xticklabels([])
    ax2.set_yticklabels([])
    
    
    ax3 = plt.subplot(233) # creates first axis
    ax3.set_xticks([0,2000,500,1000,1500])
    ax3.set_yticks([0,2000,500,1000,1500])
    ax3.imshow(mouchetis[::-1,::],alpha=0.6)
    ax3.imshow(V_exp_invmaskC,cmap='hot',extent=(X.min(),2015,Y.min(),2015), alpha=0.4)
    i3 = ax3.imshow(V_maskC,cmap='hot',extent=(X.min(),2015,Y.min(),2015),alpha=0.8)
    ax3.set_xticklabels([])
    ax3.set_yticklabels([])
    

    #Here i add the fourth figure but i dont know how i can do...

    ax4 = plt.subplot(234)
    ax4.plot(cycles,res_meanA,'ko',label='A',markersize=5,markerfacecolor='None')
    
    ax4.legend(loc=2,prop={'size':12})
    ax4.subplots_adjust(left=0.15)
    plt.show()
    
  • Homero Esmeraldo
    Homero Esmeraldo over 4 years
    See also this example with figures using subplot2grid, which the linked GridSpec above doesn't cover.