meshgrid contour contourf
参考video: https://www.bilibili.com/video/BV1qW411A775/?spm_id_from=333.337.search-card.all.click&vd_source=d171c31a396363b8ea8c0e92a59cee6b
官方文档: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.contourf.html#matplotlib.pyplot.contourf
meshgrid 网格
- 将一行, 一列的数组扩充到2D平面
import numpy as np
import matplotlib.pyplot as pltx = np.arange(0, 11, 1)
y = np.arange(0, 11, 1)xx, yy = np.meshgrid(x, y)
print(f'xx: \n{xx}')
'''
xx:
[[ 0 1 2 3 4 5 6 7 8 9 10][ 0 1 2 3 4 5 6 7 8 9 10][ 0 1 2 3 4 5 6 7 8 9 10][ 0 1 2 3 4 5 6 7 8 9 10][ 0 1 2 3 4 5 6 7 8 9 10][ 0 1 2 3 4 5 6 7 8 9 10][ 0 1 2 3 4 5 6 7 8 9 10][ 0 1 2 3 4 5 6 7 8 9 10][ 0 1 2 3 4 5 6 7 8 9 10][ 0 1 2 3 4 5 6 7 8 9 10][ 0 1 2 3 4 5 6 7 8 9 10]]
'''
print(f'yy: \n{yy}')
'''
yy:
[[ 0 0 0 0 0 0 0 0 0 0 0][ 1 1 1 1 1 1 1 1 1 1 1][ 2 2 2 2 2 2 2 2 2 2 2][ 3 3 3 3 3 3 3 3 3 3 3][ 4 4 4 4 4 4 4 4 4 4 4][ 5 5 5 5 5 5 5 5 5 5 5][ 6 6 6 6 6 6 6 6 6 6 6][ 7 7 7 7 7 7 7 7 7 7 7][ 8 8 8 8 8 8 8 8 8 8 8][ 9 9 9 9 9 9 9 9 9 9 9][10 10 10 10 10 10 10 10 10 10 10]]
'''
contour, 轮廓, contourf, 等高线
zz = np.sin(xx) + np.cos(yy)
fig, ax = plt.subplots()
plt.contour(xx, yy, zz,)
plt.show()
fig, ax = plt.subplots()
plt.contour(xx, yy, zz, levels=3, cmap=plt.cm.rainbow) # levels 指定展示的高度, cmap 指定颜色
plt.show()
# 指定level,
fig, ax = plt.subplots()
CS = plt.contour(xx, yy, zz, cmap=plt.cm.rainbow)# 画出等高线上的高度的text,
ax.clabel(CS, inline=True, fontsize=10)
'''
文档:
https://matplotlib.org/stable/gallery/images_contours_and_fields/contour_demo.html#sphx-glr-gallery-images-contours-and-fields-contour-demo-py
'''plt.show()
fig, ax = plt.subplots()
plt.contourf(xx, yy, zz)
plt.show()