下面代码在实际操作中稍作修改即可使用
代码步骤:
3D图需要额外导入模块将默认figure图转化为3D图给出x,y的坐标数据画出网格线给出高度Z的值画出图像将颜色进行投影限制画图的坐标轴范围
代码如下:
import numpy as npimport matplotlib.pyplot as plt# 3D图需要额外导入模块from mpl_toolkits.mplot3d import Axes3D# 将默认figure图转化为3D图fig = plt.figure()ax = Axes3D(fig)# 给出x,y的坐标数据X = np.arange(-4,4,0.25)Y = np.arange(-4,4,0.25)# 画出网格线X,Y = np.meshgrid(X,Y)# 给出高度Z的值R=np.sqrt(X ** 2 + Y ** 2)Z = np.sin(R)# 画出图像 rstride:横向的分割线跨度(越小越密集) cstride:纵向的分割线跨度(越小越密集)ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'),edgecolor='black')# 将颜色进行投影# zdir后的参数决定从哪个方位进行投影 offset的参数表示投影到该方位坐标的哪个点对应的坐标平面ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap='rainbow')# 限制画图的坐标轴范围ax.set_zlim(-2,2)plt.show()
效果:
# edgecolor='None'时
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'),edgecolor='None')