python - 如何在 matplotlib 中创建非线性颜色条刻度

标签 python python-3.x matplotlib colorbar

我使用 colorcet 中的“hot_r”和“kbc”创建了一个自定义发散颜色图,如下所示:

def lin_to_diverge(cmap1, cmap2, cmapname):

    in1 = plt.cm.get_cmap(cmap1)(np.linspace(0, 1, 129))
    in2 = plt.cm.get_cmap(cmap2)(np.linspace(0, 1, 129))

    combine = np.vstack((in1, in2))
    outmap = mcolors.LinearSegmentedColormap.from_list(cmapname, combine)
    return outmap

我正在全局等值线图上绘制一些数据。此操作的要点如下:

    cmap = lin_to_diverge(cc.cm.kbc, 'hot_r', 'colorcet')

    # plot a contourplot of trends on a global map
    ax.set_global()
    ax.coastlines(linewidth=0.5)
    cbarticks = np.arange(-6.0, 7.0, 1)
    ax3.set_xticks([0, 90, 180, -90, -180], crs=ccrs.PlateCarree())
    ax4.set_xticks([0, 90, 180, -90, -180], crs=ccrs.PlateCarree())
    ax1.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
    ax3.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
    ax.contourf(xx, yy, trends, cbarticks, cmap=cmap, levels=levels_def, vmin=-12, vmax=12,
                transform=ccrs.PlateCarree(), extend='both')  # ,norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03,vmin=-12, vmax=12)

    def_levels = [np.nanmin(insignificant1), 0, np.nanmax(insignificant1)]
    ax.contourf(xx, yy, insignificant1, cbarticks, levels=def_levels, hatches=["XXXXXX", ""], linewidth='0', alpha=0,
                transform=ccrs.PlateCarree(), vmin=-12, vmax=12)
    def_levels2 = [np.nanmin(insignificant2), 0, np.nanmax(insignificant2)]
    ax.contourf(xx, yy, insignificant2, cbarticks, levels=def_levels2, hatches=["//////", ""], alpha=0,
                transform=ccrs.PlateCarree(), vmin=-12, vmax=12)
    # plt.savefig(outdir + file+"_global_day_"+str(day)+".pdf", bbox_inches='tight', dpi=500)
    # plt.savefig(outdir + file+"_global_day_"+str(day)+".png")

fig.text(0.02, 0.5, 'Latitude', ha='center', va='center', rotation='vertical')
fig.text(0.48, 0.04, 'Longitude', ha='center', va='center')
ax1.set_title('Day 90')
ax2.set_title('Day 180')
ax3.set_title('Day 270')
ax4.set_title('Day 360')

# orig_cmap = mpl.cm.seismic
# shrunk_cmap = scm(orig_cmap, start=-12, midpoint=0.75, stop=12, name='shrunk')

m = plt.cm.ScalarMappable(cmap=cmap)
m.set_array(trends)
m.set_clim(-12, 12)
fig.subplots_adjust(bottom=0.07, top=1, left=0.1, right=0.9,
                    wspace=0.11, hspace=-0.1)
cb_ax = fig.add_axes([0.9, 0.05, 0.02, 0.92])

# cbarticks = [-12, -6., -5., -4., -3., -2., -1.,  0.,  1.,  2.,  3.,  4.,  5.,  6., 12]
ticks = np.linspace(-12, 12, 9)

cbar = fig.colorbar(m, cax=cb_ax, ticks=ticks)
# cbar.ax.set_yticklabels(cbarticks)
cbar.set_label('Trend [DU/year]')

plt.show()
plt.close()

我希望向颜色条添加非线性刻度,特别是不修改颜色图,因为绘图上当前颜色的分布是正确的。做这个的最好方式是什么?我应该专门为颜色条创建一个新的颜色图并从中派生刻度吗?但我不想改变颜色条目前的外观。我希望有更多接近零的值,即 [3, 2, 1.5, 0.5, 0, -0.5, -1.5, -2, -3],但这些值应该分散得更多,而位置12 和-12 目前应该保持不变。因此,接近于零的刻度应该分布得更远。

以下是根据上述脚本生成的数字:

最佳答案

解决了我的问题- 这个问题的解决办法是使用 SymLogNorm将数值缩放到更接近零,从而实现更多颜色条刻度接近零标记的预期结果。 SymLogNorm 也可以用于负值,因此它回答了这个问题。

可以使用函数的 vmin 和 vmax 参数来更改其他参数,如下所示:

matplotlib.colors.SymLogNorm(linthresh, linscale=1.0, vmin=None, vmax=None, clip=False)

我通过添加“norm=”参数将其实现到上面的代码中,如下所示:

ax.contourf(xx, yy, trends, cbarticks, cmap=cmap, levels=levels_def,
                norm=mpcol.SymLogNorm(linthresh=0.03, linscale=0.03),
                transform=ccrs.PlateCarree(), extend='both')

然后,我编辑了颜色条的刻度参数,因为对数刻度将其压缩:

cbarticks = [-12, -4, -2, -1, -0.5, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.5, 1, 2, 4, 12]
ticks = np.linspace(-12, 12, 9)
cbar = fig.colorbar(m, cax=cb_ax, ticks=cbarticks)
cbar.ax.set_yticklabels(cbarticks)

这产生了这些图:

SymLogNorm scaling of first plot in question

SymLogNorm scaling of second plot in question

关于python - 如何在 matplotlib 中创建非线性颜色条刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59276939/

相关文章:

python - 如何在 python 中迭代给定日期的几个小时?

python-3.x - 如何根据同一数据框中的另一列计算 Pandas 数据框中的值

python - 使用不相等的值列表创建 Pandas 数据框

matplotlib - 在 VisPy 中高效地绘制多条线

python - matplotlib 只有工作日没有周末在 x 轴上与 plot_date

python - 支持向量回归 (SVR) 在 Ubuntu 18.04 LTS 中未绘制任何图形

java - 哪个操作系统更适合开发 : Debian or Ubuntu?

python - Django QuerySet/Manager 从生日开始按年龄过滤员工

python - 在注入(inject)方法中访问私有(private)变量 - python

python - 散点图中的黑点