Python中scatter函數(shù)參數(shù)及用法詳解
最近開始學(xué)習(xí)Python編程,遇到scatter函數(shù),感覺(jué)里面的參數(shù)不知道什么意思于是查資料,最后總結(jié)如下:
1、scatter函數(shù)原型

2、其中散點(diǎn)的形狀參數(shù)marker如下:

3、其中顏色參數(shù)c如下:

4、基本的使用方法如下:
#導(dǎo)入必要的模塊
import numpy as np
import matplotlib.pyplot as plt
#產(chǎn)生測(cè)試數(shù)據(jù)
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#設(shè)置標(biāo)題
ax1.set_title('Scatter Plot')
#設(shè)置X軸標(biāo)簽
plt.xlabel('X')
#設(shè)置Y軸標(biāo)簽
plt.ylabel('Y')
#畫散點(diǎn)圖
ax1.scatter(x,y,c = 'r',marker = 'o')
#設(shè)置圖標(biāo)
plt.legend('x1')
#顯示所畫的圖
plt.show()
結(jié)果如下:

5、當(dāng)scatter后面參數(shù)中數(shù)組的使用方法,如s,當(dāng)s是同x大小的數(shù)組,表示x中的每個(gè)點(diǎn)對(duì)應(yīng)s中一個(gè)大小,其他如c,等用法一樣,如下:
(1)、不同大小
#導(dǎo)入必要的模塊
import numpy as np
import matplotlib.pyplot as plt
#產(chǎn)生測(cè)試數(shù)據(jù)
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#設(shè)置標(biāo)題
ax1.set_title('Scatter Plot')
#設(shè)置X軸標(biāo)簽
plt.xlabel('X')
#設(shè)置Y軸標(biāo)簽
plt.ylabel('Y')
#畫散點(diǎn)圖
sValue = x*10
ax1.scatter(x,y,s=sValue,c='r',marker='x')
#設(shè)置圖標(biāo)
plt.legend('x1')
#顯示所畫的圖
plt.show()

(2)、不同顏色
#導(dǎo)入必要的模塊
import numpy as np
import matplotlib.pyplot as plt
#產(chǎn)生測(cè)試數(shù)據(jù)
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#設(shè)置標(biāo)題
ax1.set_title('Scatter Plot')
#設(shè)置X軸標(biāo)簽
plt.xlabel('X')
#設(shè)置Y軸標(biāo)簽
plt.ylabel('Y')
#畫散點(diǎn)圖
cValue = ['r','y','g','b','r','y','g','b','r']
ax1.scatter(x,y,c=cValue,marker='s')
#設(shè)置圖標(biāo)
plt.legend('x1')
#顯示所畫的圖
plt.show()
結(jié)果:

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

注: 這就是scatter基本的用法。
PS:下面舉個(gè)示例
本文記錄了python中的數(shù)據(jù)可視化——散點(diǎn)圖scatter,令x作為數(shù)據(jù)(50個(gè)點(diǎn),每個(gè)30維),我們僅可視化前兩維。labels為其類別(假設(shè)有三類)。
這里的x就用random來(lái)了,具體數(shù)據(jù)具體分析。
label設(shè)定為[1:20]->1, [21:35]->2, [36:50]->3,(python中數(shù)組連接方法:先強(qiáng)制轉(zhuǎn)為list,用+,再轉(zhuǎn)回array)
用matplotlib的scatter繪制散點(diǎn)圖,legend和matlab中稍有不同,詳見(jiàn)代碼。
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):

figure(2):

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python中關(guān)于os.path.pardir的一些坑
這篇文章主要介紹了python中關(guān)于os.path.pardir的一些坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
python解決方案:WindowsError: [Error 2]
使用Python的rename()函數(shù)重命名文件時(shí)出現(xiàn)問(wèn)題,提示 WindowsError: [Error 2] 錯(cuò)誤,需要的朋友可以參考下2016-08-08
python 判斷是否為正小數(shù)和正整數(shù)的實(shí)例
這篇文章主要介紹了python 判斷是否為正小數(shù)和正整數(shù)的實(shí)例的相關(guān)資料,這里提供實(shí)例,實(shí)例注釋說(shuō)明很清楚,需要的朋友可以參考下2017-07-07
解決python3中os.popen()出錯(cuò)的問(wèn)題
在本篇文章里小編給大家整理的是一篇關(guān)于解決python3中os.popen()出錯(cuò)的問(wèn)題的相關(guān)內(nèi)容,有興趣的朋友們可以參考下。2020-11-11
PyTorch實(shí)現(xiàn)手寫數(shù)字識(shí)別的示例代碼
本文主要介紹了PyTorch實(shí)現(xiàn)手寫數(shù)字識(shí)別的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下<BR>2022-05-05
python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(一)
這篇文章主要介紹了python網(wǎng)絡(luò)編程基礎(chǔ)知識(shí),需要的朋友可以參考下2014-06-06
Python中的np.setdiff1d()函數(shù)詳解
Python中的np.setdiff1d()函數(shù)可用于找出兩個(gè)序列集合中元素的差異,下面通過(guò)示例代碼給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧2024-06-06

