zsh2270365028的个人博客分享 http://blog.sciencenet.cn/u/zsh2270365028

博文

[转载]pcolormesh

已有 3364 次阅读 2021-9-16 14:30 |个人分类:Python|系统分类:科研笔记|文章来源:转载

refer to: https://matplotlib.org/stable/gallery/images_contours_and_fields/pcolormesh_levels.html#sphx-glr-gallery-images-contours-and-fields-pcolormesh-levels-py

axes.Axes.pcolormesh allows you to generate 2D image-style plots. Note it is faster than the similar pcolor.

import matplotlib.pyplot as pltfrom matplotlib.colors import BoundaryNormfrom matplotlib.ticker import MaxNLocatorimport numpy as np

Basic pcolormesh

We usually specify a pcolormesh by defining the edge of quadrilaterals and the value of the quadrilateral. Note that here x and y each have one extra element than Z in the respective dimension.

np.random.seed(19680801)Z = np.random.rand(6, 10)x = np.arange(-0.5, 10, 1)  # len = 11y = np.arange(4.5, 11, 1)  # len = 7fig, ax = plt.subplots()ax.pcolormesh(x, y, Z)

pcolormesh levels

Out:

<matplotlib.collections.QuadMesh object at 0x7fe66cfa8310>

Non-rectilinear pcolormesh

Note that we can also specify matrices for X and Y and have non-rectilinear quadrilaterals.

x = np.arange(-0.5, 10, 1)  # len = 11y = np.arange(4.5, 11, 1)  # len = 7X, Y = np.meshgrid(x, y)X = X + 0.2 * Y  # tilt the coordinates.Y = Y + 0.3 * Xfig, ax = plt.subplots()ax.pcolormesh(X, Y, Z)

pcolormesh levels

Out:

<matplotlib.collections.QuadMesh object at 0x7fe66cfb3eb0>

Centered Coordinates

Often a user wants to pass X and Y with the same sizes as Z toaxes.Axes.pcolormesh. This is also allowed if shading='auto' is passed (default set by rcParams["pcolor.shading"] (default: 'flat')). Pre Matplotlib 3.3,shading='flat' would drop the last column and row of Z; while that is still allowed for back compatibility purposes, a DeprecationWarning is raised. If this is really what you want, then simply drop the last row and column of Z manually:

x = np.arange(10)  # len = 10y = np.arange(6)  # len = 6X, Y = np.meshgrid(x, y)fig, axs = plt.subplots(2, 1, sharex=True, sharey=True)axs[0].pcolormesh(X, Y, Z, vmin=np.min(Z), vmax=np.max(Z), shading='auto')axs[0].set_title("shading='auto' = 'nearest'")axs[1].pcolormesh(X, Y, Z[:-1, :-1], vmin=np.min(Z), vmax=np.max(Z),
                  shading='flat')axs[1].set_title("shading='flat'")

shading='auto' = 'nearest', shading='flat'

Out:

Text(0.5, 1.0, "shading='flat'")

Making levels using Norms

Shows how to combine Normalization and Colormap instances to draw "levels" in axes.Axes.pcolor, axes.Axes.pcolormeshand axes.Axes.imshow type plots in a similar way to the levels keyword argument to contour/contourf.

# 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()

pcolormesh with levels, contourf with levels

References

The use of the following functions, methods, classes and modules is shown in this example:

Total running time of the script: ( 0 minutes 1.147 seconds)

Download Python source code: pcolormesh_levels.py

Download Jupyter notebook: pcolormesh_levels.ipynb




https://blog.sciencenet.cn/blog-587102-1304457.html

上一篇:[转载]jupyter notebook 恢复误删单元格或者历史代码
下一篇:[转载]Jupyter Notebook安装 侧边导航栏功能 操作及其他常用扩展功能介绍
收藏 IP: 119.78.226.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (2 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-11-20 08:47

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部