python
复制代码
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
#解决中文乱码和负号不显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] =False
fig = plt.figure()
ax = plt.axes(projection='3d')
#构造3个散点向量
x1=[[-0.0221,-0.0543,-0.0525],
[0.0268,-0.0475,-0.04],
[0.0733,-0.0537,-0.0142],
[0.0645,0.0333,0.0796],
[0.0063,0.0067,0.0573],
[0.049,-0.055,0.0439],
[-0.0443,-0.0177,0.0707]]
x2=[[-9.95538197e-03 ,-6.80566940e-04 ,-3.88511945e-03],
[-3.16788722e-03 ,-2.95998948e-03 , 2.08909181e-03],
[ 6.32466376e-02 ,-1.31060611e-02 , 3.58115090e-03],
[-1.45574491e-02, -5.22264512e-03 ,-4.00055340e-03],
[-1.05448766e-02 , 3.62097926e-05, -1.25675567e-03],
[ 4.52933321e-03 ,-3.12502217e-03 , 4.04007733e-03],
[-1.10306684e-02, 3.10038822e-03 , 1.03393774e-02],
[-8.50945991e-03 , 1.84351373e-02 ,2.85878927e-02]]
x3=[[ 0.00024847, 0.00895313 , 0.00876404],
[-0.02710083 , 0.03087348 ,-0.02424021],
[-0.01565485 , 0.0263345 , -0.02861422],
[ 0.02012201 ,-0.00851349 ,-0.01320678],
[-0.0131679 , 0.0031796 , -0.00676345],
[ 0.01457987 , 0.02919558 , 0.01558889],
[ 0.01512623 ,-0.00254833 , 0.00778025],
[ 0.04662133 , 0.01249118 , 0.0103233 ]]
for i in x1:
ax.scatter(i[0],i[1], i[2],c='black')
for i in x2:
ax.scatter(i[0],i[1], i[2],c='blue')
for i in x3:
ax.scatter(i[0],i[1], i[2],c='red')
#添加标题
ax.set_title(' this is a title')
#增加图例
ax.text(0.08,0.10,0.13, '蓝色:label2', fontsize = 8,color='blue')
ax.text(0.08,0.10,0.12, '黑色:label1', fontsize = 8,color='black')
ax.text(0.08,0.10,0.11, '红色:label3', fontsize = 8,color='red')
plt.show()