2013-11-01 3 views
9

scikit-learn에서 KMeans 알고리즘을 통해 생성하는 플롯이 있습니다. 클러스터는 다른 색상에 해당합니다. 줄거리는 다음과 같습니다. enter image description here범례를 matplotlib의 분산 점 색상에 일치시킵니다.

플롯의 클러스터 번호에 해당하는 범례가 필요합니다. 이상적으로, 범례에는 클러스터의 색상이 표시되어야하며 레이블은 클러스터 번호 여야합니다. 감사.

편집 : 나는 사람들이 내가 전설이 색상에 해당 할 수 있었다이

from sklearn.cluster import KMeans 
km = KMeans(n_clusters=20, init='random') 
km.fit(df) #df is the dataframe which contains points as coordinates 
labels = km.labels_ 
plt.clf() 
fig = plt.figure() 
ax = fig.add_subplot(111, axisbg='w', frame_on=True) 
fig.set_size_inches(18.5, 10.5) 

# Plot the clusters on the map 
# m is a basemap object 
m.scatter(
     [geom.x for geom in map_points], 
     [geom.y for geom in map_points], 
     20, marker='o', lw=.25, 
     c = labels.astype(float), 
     alpha =0.9, antialiased=True, 
     zorder=3) 
m.fillcontinents(color='#555555') 
plt.show() 
+0

() '명령을, 또는 다른 메이크업으로 여기

코드입니다 모든 클래스가있는 개별 색상 막대. Sklearn 갤러리에서이를 수행하는 방법을 보여주는 예제가 있습니다. 사람들은 귀하의 모범을 실행할 수 없으므로 함께 일하기가 어렵습니다. –

+0

Skullarn 갤러리 – Nitin

+1

@ Niitin을 확인합니다. 솔루션을 생각해 내면 여기에 다시 게시하십시오. – cd98

답변

11

을 downvoting 때문에 나는 몇 가지 코드를 넣어한다고 생각합니다. 열쇠는 Rutger Kassies가 언급 한 데이터의 각 카테고리에 대해 여러 개의 산점도를 사용했습니다.

import numpy as np 
import matplotlib.pyplot as plt 

# Setting various plot properties 
plt.clf() 
fig = plt.figure() 
ax = fig.add_subplot(111, axisbg='w', frame_on=True) 
fig.set_size_inches(18.5, 10.5) 

# Creating a discrete colorbar 
colors = plt.cm.rainbow(np.linspace(0, 1, 20)) 

current_plot_range = 0 
previous_plot_range = 0 

for i,c in enumerate(colors): 
    previous_plot_range += current_plot_range 
    current_plot_range = labels[labels==i].size 
    m.scatter(
     [geom.x for geom in map_points[  
      previous_plot_range:previous_plot_range+current_plot_range]], 
     [geom.y for geom in map_points[ 
      previous_plot_range:previous_plot_range+current_plot_range]], 
     20, lw=.25, marker='o',color = c, label=i, alpha =0.9, antialiased=True, 
     zorder=3) 

plt.legend() 
m.fillcontinents(color='#555555') 

결과이 같은 같습니다 : 당신은 별도의`m.scatter 각 클러스터를 그릴 수 enter image description here

+0

답변 해 주셔서 감사합니다. zip 대신에 enumerate를 사용하는 것을 고려할 수 있습니다. – SeF

관련 문제