python中的3d画图

python的3d散点图

from sklearn import KMeans

from sklearn.externals import joblib
from sklearn import cluster
import numpy as np 
# 生成10*3的矩阵
data = np.random.rand(100,3)
print data
# 聚类为4类
estimator=KMeans(n_clusters=3)
# fit_predict表示拟合+预测,也可以分开写
res=estimator.fit_predict(data)
# 预测类别标签结果
lable_pred=estimator.labels_
# 各个类别的聚类中心值
centroids=estimator.cluster_centers_
# 聚类中心均值向量的总和
inertia=estimator.inertia_
 
print lable_pred
print centroids
print inertia

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure()
ax = plt.subplot(111,projection = "3d")
X = data[:,0]
Y = data[:,1]
Z = data[:,2]
C = []
for i in range(len(data)):
    if int(lable_pred[i]) == 0:
        C.append('r')
    if int(lable_pred[i]) == 1:   
        C.append('b')
    if int(lable_pred[i]) == 2:   
        C.append('black')
ax.scatter(X,Y,Z,c=C)
ax.set_xlabel('longitude')
ax.set_ylabel('latitude')
ax.set_zlabel('deepth')
plt.show()




猜你喜欢

转载自blog.csdn.net/yibo492387/article/details/78783399
今日推荐