Search code examples
pythonmatplotlibcolorbar

How to show colorbar on each individual matshow subplot


I am creating a (10,7) subplot of multiple different gridded fields. The following code is what is being currently used:

fig, axes = plt.subplots(nrows=10, ncols=7, figsize=(18, 16), dpi= 100, 
facecolor='w', edgecolor='k')
titles = ['Z1','Z2','Z3','ZDR1','ZDR2','ZDR3','Dist']
for i in range(0,10):
    z = 1*10+i
    for j in range(0,7):
        aa = axes[i,j].matshow(alldata_sim[z,:,:,j], cmap='jet')
        fig.colorbar(aa)
        axes[0,j].set_title(titles[j])
        axes[i,j].get_xaxis().set_visible(False)
        axes[i,j].get_yaxis().set_ticks([])
        axes[i,0].set_ylabel(allgauge_sim[z])

Which produces the following figure:

Figure1

The question is: how do I get the colorbars to be on the right-hand side of each respective individual subplot?


Solution

  • maybe try changing

    fig.colorbar(aa)
    

    to

    fig.colorbar(aa,ax=axes[i,j])
    

    Hope it helps!