参考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
import numpy as np
import matplotlib.pyplot as plt
x = 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]]
'''
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()