matplotlib - How to make a scatter plot for clustering in Python -
i carrying out clustering , try plot result. dummy data set :
data
import numpy np x = np.random.randn(10) y = np.random.randn(10) cluster = np.array([0, 1, 1, 1, 3, 2, 2, 3, 0, 2]) # labels of cluster 0 3 cluster center
centers = np.random.randn(4, 2) # 4 centers, each center 2d point question
i want make scatter plot show points in data , color points based on cluster labels.
then want superimpose center points on same scatter plot, in shape (e.g. 'x') , fifth color (as there 4 clusters).
comment
- i turned seaborn 0.6.0 found no api accomplish task.
- ggplot yhat made scatter plot nice second plot replace first one.
- i got confused
color,cmapin matplotlib wonder if use seaborn or ggplot it.
the first part of question can done using colorbar , specifying colours cluster array. have vaguely understood second part of question, believe looking for.
import numpy np import matplotlib.pyplot plt x = np.random.randn(10) y = np.random.randn(10) cluster = np.array([0, 1, 1, 1, 3, 2, 2, 3, 0, 2]) # labels of cluster 0 3 centers = np.random.randn(4, 2) fig = plt.figure() ax = fig.add_subplot(111) scatter = ax.scatter(x,y,c=cluster,s=50) i,j in centers: ax.scatter(i,j,s=50,c='red',marker='+') ax.set_xlabel('x') ax.set_ylabel('y') plt.colorbar(scatter) fig.show() which results in:

wherein "centres" have been shown using + marker. can specify colours want them in same way have done x , y
Comments
Post a Comment