【发布时间】:2019-07-15 15:18:45
【问题描述】:
我正在尝试为散点图中的集群着色,并使用两种不同的方法进行管理。
在第一个中,我迭代地绘制每个集群,在第二个中,我一次绘制所有数据并根据它们的标签 [0, 1, 2, 3 ,4] 为集群着色。
我对在example1 和example3 中得到的结果很满意,但我不明白为什么在根据标签为集群着色而不是迭代地绘制每个集群时,颜色会发生如此巨大的变化。
另外,为什么第二个集群(尽管标签总是“1”)在 example1 和 example3 中具有不同的颜色?
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight') #irrelevant here, but coherent with the examples=)
fig, ax = plt.subplots(figsize=(6,4))
for clust in range(kmeans.n_clusters):
ax.scatter(X[kmeans.labels_==clust],Y[kmeans.labels_==clust])
ax.set_title("example1")`
和
plt.figure(figsize = (6, 4))
plt.scatter(X,Y,c=kmeans.labels_.astype(float))
plt.title("example2")
(我知道我可以为第二种方法显式定义颜色图,但我找不到任何重现示例 1 中结果的颜色图)
这是一个最小的工作示例
import matplotlib.pyplot as plt
import pandas as pd
plt.style.use('fivethirtyeight') #irrelevant here, but coherent with the examples=)
X=pd.Series([1, 2, 3, 4, 5, 11, 12, 13, 14, 15])
Y=pd.Series([1,1,1,1,1,2,2,2,2,2])
clusters=pd.Series([0,0,0,0,0,1,1,1,1,1])
fig, ax = plt.subplots(figsize=(6,4))
for clust in range(2):
ax.scatter(X[clusters==clust],Y[clusters==clust])
ax.set_title("example3")
plt.figure(figsize = (6, 4))
plt.scatter(X,Y, c=clusters)
plt.title("example4")
【问题讨论】:
标签: python matplotlib scatter-plot graph-coloring