可视化3D群集

时间:2018-07-04 14:47:20

标签: python matplotlib cluster-analysis data-visualization

因此,我想在3d图形上可视化3个群集。我不确定如何添加第三个轴。

X = np.array(X)
plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], s = 100, c = 'red', label = 'Cluster 1')
plt.scatter(X[y_kmeans == 1, 0], X[y_kmeans == 1, 1], s = 100, c = 'blue', label = 'Cluster 2')
plt.scatter(X[y_kmeans == 2, 0], X[y_kmeans == 2, 1], s = 100, c = 'green', label = 'Cluster 3')
plt.xlabel("Recency")
plt.ylabel("Frequency")
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1],s = 300, c = 'yellow', label = 'Centroids')
plt.show()

这是我所做的,但我知道这仅适用于2d。 y_kmeans包含与我的X数据集中的行号相对应的群集。 X数据集有3列。 我想知道是否有人可以指导我如何做到这一点? 更新: 可以在以下答案的帮助下使其正常工作。

1 个答案:

答案 0 :(得分:1)

随时适应您的需求

from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D
import random


fig = pyplot.figure()
ax = Axes3D(fig)

x_vals = np.random.rand(1000)
y_vals = np.random.rand(1000)
z_vals = np.random.rand(1000)



ax.scatter(x_vals, y_vals, z_vals, color='red')
ax.scatter(x_vals+0.2, y_vals-0.8, z_vals, color='blue')
pyplot.show()

输出:

enter image description here