作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试对我的结果进行聚类。我使用 matplotlib 进入 3 个集群以及标签名称:
Y_sklearn
- 包含 X 和 Y 坐标的二维数组
ent_names
- 包含标签名称
我有一个逻辑来显示散点图如下:
from sklearn.cluster import KMeans
model = KMeans(n_clusters = 3)
model.fit(Y_sklearn)
plt.scatter(Y_sklearn[:,0],Y_sklearn[:,1], c=model.labels_);
plt.show()
但是,除了这个图,我还想显示标签名称。我试过这样的东西,但它只显示一种颜色:
with plt.style.context('seaborn-whitegrid'):
plt.figure(figsize=(8, 6))
for lab, col in zip(ent_names,
model.labels_):
plt.scatter(Y_sklearn[y==lab, 0],
Y_sklearn[y==lab, 1],
label=lab,
c=model.labels_)
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.legend(loc='lower center')
plt.tight_layout()
plt.show()
最佳答案
您必须在相同的坐标轴 ax
中绘制才能将散点图放在一起,如本例所示:
import matplotlib.pyplot as plt
import numpy as np
XY = np.random.rand(10,2,3)
labels = ['test1', 'test2', 'test3']
colors = ['r','b','g']
with plt.style.context('seaborn-whitegrid'):
plt.figure(figsize=(8, 6))
ax = plt.gca()
i=0
for lab,col in zip(labels, colors):
ax.scatter(XY[:,0,i],XY[:,1,i],label=lab, c=col)
i+=1
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.legend(loc='lower center')
plt.tight_layout()
plt.show()
关于python - 使用 Python 中的聚类进行 ScatterPlot 着色和标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46195547/
我是一名优秀的程序员,十分优秀!