1、3D曲面图
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
# 从mpl_toolkits.mplot3d导入Axes3D
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure()
ax = Axes3D(fig)
# 定义3D坐标轴
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X ** 2 + Y ** 2)
Z = np.sin(R)
# 定义三维数据
ax.plot_surface(X, Y, Z, rstride=2, cstride=1, cmap= 'rainbow')
# 绘制曲面图,用 cmap 着色
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.cm.hot)
# 绘制等高线图,要设置offset,为Z的最小值
ax.set_zlim(-2, 2)
# Z的范围
plt.title("3D曲面图")
plt.show()