pcolormesh
演示如何组合Normalization和Colormap实例以在pcolor(),pcolormesh()和imshow()类型图中绘制“级别”,其方式与contour / contourf的levels关键字参数类似。
import matplotlibimport matplotlib.pyplot as pltfrom matplotlib.colors import BoundaryNormfrom matplotlib.ticker import MaxNLocatorimport numpy as np# make these smaller to increase the resolutiondx, dy = 0.05, 0.05# generate 2 2d grids for the x & y boundsy, x = np.mgrid[slice(1, 5 + dy, dy),slice(1, 5 + dx, dx)]z = np.sin(x)**10 + np.cos(10 + y*x) * np.cos(x)# x and y are bounds, so z should be the value *inside* those bounds.# Therefore, remove the last value from the z array.z = z[:-1, :-1]levels = MaxNLocator(nbins=15).tick_values(z.min(), z.max())# pick the desired colormap, sensible levels, and define a normalization# instance which takes data values and translates those into levels.cmap = plt.get_cmap('PiYG')norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)fig, (ax0, ax1) = plt.subplots(nrows=2)im = ax0.pcolormesh(x, y, z, cmap=cmap, norm=norm)fig.colorbar(im, ax=ax0)ax0.set_title('pcolormesh with levels')# contours are *point* based plots, so convert our bound into point# centerscf = ax1.contourf(x[:-1, :-1] + dx/2.,y[:-1, :-1] + dy/2., z, levels=levels,cmap=cmap)fig.colorbar(cf, ax=ax1)ax1.set_title('contourf with levels')# adjust spacing between subplots so `ax1` title and `ax0` tick labels# don't overlapfig.tight_layout()plt.show()

参考
下面的示例演示了以下函数和方法的使用:
matplotlib.axes.Axes.pcolormeshmatplotlib.pyplot.pcolormeshmatplotlib.axes.Axes.contourfmatplotlib.pyplot.contourfmatplotlib.figure.Figure.colorbarmatplotlib.pyplot.colorbarmatplotlib.colors.BoundaryNormmatplotlib.ticker.MaxNLocator
