使用matplotlib画一个$\sqrt{x^2 + y^2}$的三维网格图,参考代码如下,
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = plt.axes(projection="3d")
x = np.arange(-5, 5, 0.1)
y = np.arange(-5, 5, 0.1)
z = np.sqrt(x**2+y**2)
X, Y = np.meshgrid(x, y) # 2生成绘制3D图形所需的网络数据
Z = np.sqrt(X**2+Y**2)
ax.plot3D(x,y,z,'gray')
ax.plot_surface(X, Y, Z, alpha=0.5, cmap="winter") # 生成表面,alpha用于控制透明度
ax.set_xlabel("X") # 设置X、Y、Z 轴及坐标范围
ax.set_xlim(-6, 6)
ax.set_ylabel("Y")
ax.set_ylim(-6, 6)
ax.set_zlabel("Z")
plt.title('Image plot of $\sqrt{x^2 + y^2}$ for a grid of values')
plt.show()