简体   繁体   中英

Color bar limits matplotlib

I'm trying to create a plot with 4 hist2d subplots and one color bar. The thing is that each subplot can have different ranges of z values, so the color bar is not uniform. I want to set the color bar to a pre-defined range.

here is the code I'm using:

def multiple_med_plot_test(file):
    extent = [-8, 37, 28, 46]
    fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(26, 11), constrained_layout=True,
                             subplot_kw={'projection': ccrs.PlateCarree()})
    ax0 = axes[0][0]
    ax1 = axes[0][1]
    ax2 = axes[1][0]
    ax3 = axes[1][1]
    axes_dict = {'Dec': ax0, 'Aug': ax1, 'Sep': ax2, 'Sum': ax3}

    for month in axes_dict.keys():
        ax = axes_dict[month]
        ax.add_feature(cfeature.LAND, edgecolor='k', zorder=50)
        ax.set_extent(extent)
        gl = ax.gridlines(draw_labels=True, zorder=100, color='grey', linestyle='--')
        gl.top_labels = False
        gl.right_labels = False
        gl.xlabel_style = {'size': 16}
        gl.ylabel_style = {'size': 16}
        if ax in [ax1, ax3]:
            gl.left_labels = False
        ax.set_title(month, fontsize=18, color='darkred')
        if month != 'Sum':
            hist0 = ax.hist2d(file.Long, file.Lat, range=[(-8, 37), (28, 46)], bins=(500, 200))
        elif month == 'Sum':
            hist1 = ax.hist2d(file.Long, file.Lat, range=[(-8, 37), (28, 46)], bins=(500, 200))

    fig.suptitle('Lightning Density per Month', fontsize=22)
    cbar = fig.colorbar(hist1[3], ax=axes, shrink=0.95)
    cbar.set_label('# of lightnings', fontsize=20, rotation=-90, labelpad=30)
    cbar.ax.tick_params(labelsize=16)
    # plt.savefig('D:/Lightning Data/Yearly_Summary', dpi=100)
    plt.show()

In previous versions of the code I used plt.clim and that was awesome, but the way my code is right now doesn't let me do it.

I would like to get some help on this!

If you want a linear scale, set vmin and vmax parameters. For log-like scale or similar, use norm . See hist2d documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM