简体   繁体   中英

How can i color each cluster?

I tried to make clustering for my data, the code is

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from sklearn.manifold import MDS
import itertools
import matplotlib.cm
from sklearn import manifold

xx = pd.read_csv('distances.data',sep=" ",header=None)
NL=max(xx[0])
ND=max(xx[1])
if NL>ND:
    ND=NL 
N = len(xx)    
dist=np.zeros([ND,ND])
for i in range(N):
    ii = xx[0][i]-1
    jj = xx[1][i]-1
    dist[ii][jj]=xx[2][i]
    dist[jj][ii]=xx[2][i]

print("start")
mds = manifold.MDS(max_iter=200, eps=1e-4, n_init=1,dissimilarity='precomputed',random_state=0)
dp_mds = mds.fit_transform(dist)
plt.scatter(dp_mds[:, 0], dp_mds[:, 1])
plt.savefig("1.jpg")

and got the shape like this for my data enter image description here but need each cluster has a different color, how can i do it, please?

can i get like that enter image description here

You'll want to utilize c and cmap :

colors = dict(c=dp_mds[:, 0], cmap=plt.cm.get_cmap('jet', 6))
plt.scatter(dp_mds[:, 0], dp_mds[:, 1], **colors)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM