一:利用的包:
(1)构建三维对象:mpl_toolkits.mplot3d里面导入Axes3D
(2)数据方面操作:numpy
(3)绘图工具包:matplotlib.pyplot

二:绘图:
1、绘图主要分成两种情况:
(1)一种是根据函数来绘制三维图
(2)一种是根据三维坐标绘制散点图

2、代码一:绘制散点图(加颜色,修饰什么的省略)

import matplotlib.pyplot as pltimport numpy as npfrom mpl_toolkits.mplot3d import Axes3Dimport csv#读取数据datasets=[]with open(r'a.csv','rt') as f: lines=csv.reader(f) for line in lines: datasets.append(line)#获取有用部分datesets=datasets[1:]#转化为array数组,便于列数据的切片获取datasets=np.array(datasets)#数据类型转换一下,以防止画图时精度缺失报错X=datasets[:,-3].astype('float32')Y=datasets[:,-2].astype('float32')Z=datasets[:,-1].astype('float32')ax=plt.subplot(111,projection='3d')ax.scater(X,Y,Z)ax.set_zlabel('z')ax.set_ylabel('y')ax.set_xlabel('x')plt.show()

代码二:以z=x+y2为例画出三维图:

from mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltimport nmpy as npfig=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)R=np.sqrt(X+Y**2)Z=np.sin(R)ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap='rainbow')