剛開始接觸Python,照着例子寫的代碼,百度注釋的。
from numpy import * import matplotlib import matplotlib.pyplot as plt import numpy as np x = random.rand(50,30) #創建一個50行30列的多維數組(ndarray) #basic f1 = plt.figure(1) #創建顯示圖形輸出的窗口對象 plt.subplot(211) #創建子坐標系 #x[:,1]獲取第二列作為一維數組,x[:,0]獲取第一列作為一維數組 plt.scatter(x[:,1],x[:,0]) #畫散點圖 # with label plt.subplot(212) #c重新創建坐標系 #list(ones(20)) 創建1行20列的全為1列表 #list(2*ones(15)) 創建1行15列全為2的列表 #list(3*ones(15) 創建1行15列全為3的列表 label = list(ones(20))+list(2*ones(15))+list(3*ones(15)) #將列表合並到一起,共50列 label = array(label) #將列表轉為數組 #15.0*label 將數組的每個值都乘以15.0 #x[:,1] 將x的第2列50行轉為1行50列 #x[:,0] 將x的第1列50行轉為1行50列 #x軸和y軸均50個點,兩個Label都是1行50列的數組 #從第一個點到第20個點的樣式相同,從第21到第35個點相同,從第36到第50個點相同 plt.scatter(x[:,1],x[:,0],15.0*label,15.0*label) # with legend f2 = plt.figure(2) #創建顯示圖形輸出的窗口對象 idx_1 = np.where(label==1) #找label中為1的位置 #畫圖 marker標識散點圖樣式 color標識顏色 label表示圖例的解釋 s表示散點的大小 p1 = plt.scatter(x[idx_1,1], x[idx_1,0], marker = 'x', color = 'm', label='1', s = 30) idx_2 = np.where(label==2) #找label中為2的位置 p2 = plt.scatter(x[idx_2,1], x[idx_2,0], marker = '+', color = 'c', label='2', s = 50) idx_3 = np.where(label==3) #找label中為3的位置 p3 = plt.scatter(x[idx_3,1], x[idx_3,0], marker = 'o', color = 'r', label='3', s = 15) plt.legend(loc = 'upper right') #圖例的位置 plt.show()