BoundaryNorm

標準のカラーマップは,値と色の対応は,最大値・最小値に基づいて行われる.このことは,pcolormeshあるいはcontourfで塗りわける値を指定しても,値に対して色は変わらないということである.このままだと,特定の値の付近を細かく見たいといった場合に向かない.塗り分ける値に対して,線形にcolormapを適用することを可能とするのが,BoundaryNormである.


The standard colormap is used for the maximum and minimum values of data. This means that if you specify values of color boundary for pcolormesh or contourf, the colors for specific values are essentially unchanged. This is not convenient if you want to differences in some value ranges. In such cases, you can use BoundaryNorm as shown in the following example.
# sample script for the use of BoundaryNormimport matplotlib.colors as colorsfrom matplotlib import pyplot as pltimport numpy as np
xs=np.arange(0, 360, 1)ys=np.arange(-90, 90.1, 1)xsz=len(xs)ysz=len(ys)xs2d=np.ones([ysz,1]) @ xs.reshape(1,xsz)ys2d=ys.reshape(ysz,1) @ np.ones([1,xsz])zs=np.cos(xs2d/180*np.pi) * np.cos(ys2d/90*np.pi)
plt.ion()plt.clf()
levels=np.array([-1., -0.5, -0.1, -0.01, 0.01, 0.1, 0.5, 1.0])cmap='RdYlBu_r'norm_b=colors.BoundaryNorm(boundaries=levels, ncolors=256)
ax1=plt.subplot(1,3,1)img1 = ax1.contourf(xs,ys,zs,cmap=cmap,levels=levels,extend='both')plt.colorbar(img1)plt.title('contourf without BoundaryNorm',fontsize=10)
ax2=plt.subplot(1,3,2)img2 = ax2.contourf(xs,ys,zs,cmap=cmap,levels=levels,norm=norm_b,extend='both')plt.colorbar(img2)plt.title('contourf without BoundaryNorm',fontsize=10)

ax3=plt.subplot(1,3,3)img3 = ax3.pcolormesh(xs,ys,zs,norm=norm_b,cmap=cmap)plt.colorbar(img3)plt.title('pcolormesh with BoundaryNorm',fontsize=10)