国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中scatter函數參數及用法詳解

2020-01-04 16:29:48
字體:
來源:轉載
供稿:網友

最近開始學習Python編程,遇到scatter函數,感覺里面的參數不知道什么意思于是查資料,最后總結如下:

1、scatter函數原型

Python,scatter函數,Python中scatter()函數

2、其中散點的形狀參數marker如下:

Python,scatter函數,Python中scatter()函數

3、其中顏色參數c如下:

Python,scatter函數,Python中scatter()函數

4、基本的使用方法如下:

#導入必要的模塊 import numpy as np import matplotlib.pyplot as plt #產生測試數據 x = np.arange(1,10) y = x fig = plt.figure() ax1 = fig.add_subplot(111) #設置標題 ax1.set_title('Scatter Plot') #設置X軸標簽 plt.xlabel('X') #設置Y軸標簽 plt.ylabel('Y') #畫散點圖 ax1.scatter(x,y,c = 'r',marker = 'o') #設置圖標 plt.legend('x1') #顯示所畫的圖 plt.show() 

結果如下:

Python,scatter函數,Python中scatter()函數

5、當scatter后面參數中數組的使用方法,如s,當s是同x大小的數組,表示x中的每個點對應s中一個大小,其他如c,等用法一樣,如下:

(1)、不同大小

#導入必要的模塊 import numpy as np import matplotlib.pyplot as plt #產生測試數據 x = np.arange(1,10) y = x fig = plt.figure() ax1 = fig.add_subplot(111) #設置標題 ax1.set_title('Scatter Plot') #設置X軸標簽 plt.xlabel('X') #設置Y軸標簽 plt.ylabel('Y') #畫散點圖 sValue = x*10 ax1.scatter(x,y,s=sValue,c='r',marker='x') #設置圖標 plt.legend('x1') #顯示所畫的圖 plt.show() 

Python,scatter函數,Python中scatter()函數

(2)、不同顏色

#導入必要的模塊 import numpy as np import matplotlib.pyplot as plt #產生測試數據 x = np.arange(1,10) y = x fig = plt.figure() ax1 = fig.add_subplot(111) #設置標題 ax1.set_title('Scatter Plot') #設置X軸標簽 plt.xlabel('X') #設置Y軸標簽 plt.ylabel('Y') #畫散點圖 cValue = ['r','y','g','b','r','y','g','b','r'] ax1.scatter(x,y,c=cValue,marker='s') #設置圖標 plt.legend('x1') #顯示所畫的圖 plt.show() 

結果:

Python,scatter函數,Python中scatter()函數

(3)、線寬linewidths

#導入必要的模塊 import numpy as np import matplotlib.pyplot as plt #產生測試數據 x = np.arange(1,10) y = x fig = plt.figure() ax1 = fig.add_subplot(111) #設置標題 ax1.set_title('Scatter Plot') #設置X軸標簽 plt.xlabel('X') #設置Y軸標簽 plt.ylabel('Y') #畫散點圖 lValue = x ax1.scatter(x,y,c='r',s= 100,linewidths=lValue,marker='o') #設置圖標 plt.legend('x1') #顯示所畫的圖 plt.show() 

Python,scatter函數,Python中scatter()函數

注:  這就是scatter基本的用法。

PS:下面舉個示例

本文記錄了python中的數據可視化——散點圖scatter,令x作為數據(50個點,每個30維),我們僅可視化前兩維。labels為其類別(假設有三類)。

這里的x就用random來了,具體數據具體分析。

label設定為[1:20]->1, [21:35]->2, [36:50]->3,(python中數組連接方法:先強制轉為list,用+,再轉回array)

用matplotlib的scatter繪制散點圖,legend和matlab中稍有不同,詳見代碼。

x = rand(50,30) from numpy import * import matplotlib import matplotlib.pyplot as plt  #basic f1 = plt.figure(1) plt.subplot(211) plt.scatter(x[:,1],x[:,0])  # with label plt.subplot(212) label = list(ones(20))+list(2*ones(15))+list(3*ones(15)) label = array(label) plt.scatter(x[:,1],x[:,0],15.0*label,15.0*label)  # with legend f2 = plt.figure(2) idx_1 = find(label==1) p1 = plt.scatter(x[idx_1,1], x[idx_1,0], marker = 'x', color = 'm', label='1', s = 30) idx_2 = find(label==2) p2 = plt.scatter(x[idx_2,1], x[idx_2,0], marker = '+', color = 'c', label='2', s = 50) idx_3 = find(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') 

result:

figure(1):

Python,scatter函數,Python中scatter()函數

figure(2):

Python,scatter函數,Python中scatter()函數

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜春市| 金沙县| 清苑县| 北海市| 响水县| 宝丰县| 甘孜| 沂水县| 神木县| 景宁| 湘潭县| 二手房| 张家川| 关岭| 千阳县| 韶关市| 眉山市| 望江县| 巴林左旗| 建昌县| 边坝县| 临清市| 浪卡子县| 宣恩县| 视频| 乡宁县| 当阳市| 锦屏县| 报价| 进贤县| 琼海市| 保山市| 中宁县| 基隆市| 中阳县| 丰都县| 凭祥市| 天峨县| 安溪县| 黄浦区| 三江|