標籤:python matplotlib
今天太晚了,參考連結先放在這裡,明天再根據自己的實際情況整體一下,http://www.cnblogs.com/qianlifeng/archive/2012/02/13/2350086.html
實際上遇到的原始碼作為例子貼上。
# show your cluster only available with 2-D data #centroids為k個類別,其中儲存著每個類別的質心#clusterAssment為樣本的標記,第一列為此樣本的類別號,第二列為到此類別質心的距離 def showCluster(dataSet, k, centroids, clusterAssment): numSamples, dim = dataSet.shape if dim != 2: print ("Sorry! I can not draw because the dimension of your data is not 2!") return 1 mark = [‘or‘, ‘ob‘, ‘og‘, ‘ok‘, ‘^r‘, ‘+r‘, ‘sr‘, ‘dr‘, ‘<r‘, ‘pr‘] if k > len(mark): print ("Sorry! Your k is too large! please contact wojiushimogui") return 1 # draw all samples for i in range(numSamples): markIndex = int(clusterAssment[i, 0]) #為樣本指定顏色 plt.plot(dataSet[i, 0], dataSet[i, 1], mark[markIndex]) mark = [‘Dr‘, ‘Db‘, ‘Dg‘, ‘Dk‘, ‘^b‘, ‘+b‘, ‘sb‘, ‘db‘, ‘<b‘, ‘pb‘] # draw the centroids for i in range(k): #畫每個類的質心點 plt.plot(centroids[i, 0], centroids[i, 1], mark[i], markersize = 12) plt.show()
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
python使用matplotlib繪圖