>RE::VISION CRM

Python데이터분석

[파이썬] kmeans scatter plot: plot different colors per cluster

YONG_X 2020. 12. 22. 12:55

파이썬 scatter plot에 클러스터 또는 그룹별로 다른 색을 칠하고 싶다면

컬러를 구분할 벡터를 먼저 생성한 후 맵을 만들어서 대응되는 색을 가져오도록 처리

예를들면 아래처럼

 

The color= or c= property should be a matplotlib color, as mentioned in the documentation for plot.

To map a integer label to a color just do

 

LABEL_COLOR_MAP = {0 : 'r', 1 : 'k', ...., }

label_color = [LABEL_COLOR_MAP[l] for l in labels]

plt.scatter(x, y, c=label_color)

 

 

 

# ........  OR

 

 

If you don't want to use the builtin one-character color names, you can use other color definitions. See the documentation on matplotlib colors.

 

 

# Scaling the data to normalize

model = KMeans(n_clusters=5).fit(X)

# Visualize it:

plt.figure(figsize=(8, 6))

plt.scatter(data[:,0], data[:,1], c=model.labels_.astype(float))

 

 

 

stackoverflow.com/questions/28227340/kmeans-scatter-plot-plot-different-colors-per-cluster