import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D #3D图模块包 #生成3D图坐标 fig = plt.figure() ax = Axes3D(fig) 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) #构建3D图 ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow')) #设置投影等高线 ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap='rainbow') ax.set_zlim(-2,2) plt.show()