matplotlib colorbar in each subplot

后端 未结 5 1040
一生所求
一生所求 2020-11-30 05:33

I would like to add a separate colorbar to each subplot in a 2x2 plot.

fig , ( (ax1,ax2) , (ax3,ax4)) = plt.subplots(2, 2,sharex = True,sharey=True)
z1_plot          


        
5条回答
  •  借酒劲吻你
    2020-11-30 06:02

    This can be easily solved with the the utility make_axes_locatable. I provide a minimal example that shows how this works and should be readily adaptable:

    import matplotlib.pyplot as plt
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    
    import numpy as np
    
    m1 = np.random.rand(3, 3)
    m2 = np.arange(0, 3*3, 1).reshape((3, 3))
    
    fig = plt.figure(figsize=(16, 12))
    ax1 = fig.add_subplot(121)
    im1 = ax1.imshow(m1, interpolation='None')
    
    divider = make_axes_locatable(ax1)
    cax = divider.append_axes('right', size='5%', pad=0.05)
    fig.colorbar(im1, cax=cax, orientation='vertical')
    
    ax2 = fig.add_subplot(122)
    im2 = ax2.imshow(m2, interpolation='None')
    
    divider = make_axes_locatable(ax2)
    cax = divider.append_axes('right', size='5%', pad=0.05)
    fig.colorbar(im2, cax=cax, orientation='vertical');
    

提交回复
热议问题