matplotlib colorbar in each subplot

后端 未结 5 1045
一生所求
一生所求 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:14

    Try to use the func below to add colorbar:

    def add_colorbar(mappable):
        from mpl_toolkits.axes_grid1 import make_axes_locatable
        import matplotlib.pyplot as plt
        last_axes = plt.gca()
        ax = mappable.axes
        fig = ax.figure
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="5%", pad=0.05)
        cbar = fig.colorbar(mappable, cax=cax)
        plt.sca(last_axes)
        return cbar
    

    Then you codes need to be modified as:

    fig , ( (ax1,ax2) , (ax3,ax4)) = plt.subplots(2, 2,sharex = True,sharey=True)
    z1_plot = ax1.scatter(x,y,c = z1,vmin=0.0,vmax=0.4)
    add_colorbar(z1_plot)
    

提交回复
热议问题