1.三维散点图
# 三维图形必须导入该模块from mpl_toolkits import mplot3d# 方式一ax = plt.axes(projection='3d')ax.scatter3D(xs, ys, zs, s, c,marker,alpha,label))# 方式二ax = plt.axes(projection='3d')ax.scatter(xs, ys, zs, s, c,marker,alpha,label)
除了Z坐标外,三维散点图和二维散点图各参数意义一致,具体取值详见matplotlib库:散点图。在设置坐标轴为3d后,scatter与scatter3D两个函数是等价的。事实上,带有projection参数的相关函数均可生成三维图形的轴对象如subplot(projection='3d'),add_subplot(projection='3d')等,在此基础上均可调用相关函数生成对应的三维图形。
from mpl_toolkits import mplot3dimport matplotlib.pyplot as plt# 数据x = [1, 2, 6, 7, 4, 8, 5]y = [2, 4, 1, 5, 6, 7, 3]z = [3, 6, 2, 7, 9, 8, 2]ax = plt.axes(projection='3d')ax.scatter(x, y, z, c=z, s=200, cmap='jet')ax.set_title('三维散点图', fontproperties='STSong')ax.set_xlabel('X', fontsize=14)ax.set_ylabel('Y', fontsize=14)ax.set_zlabel('Z', fontsize=14)plt.tight_layout()plt.show()
![183cf4ee70d5160ea178071fb9395c3a.png](https://i-blog.csdnimg.cn/blog_migrate/3922727e2f097a82e63b2d25a027d37d.jpeg)