散點(diǎn)圖
Scatter chart(PointGraph, X-Y Plot, Scatter Chart或者 Scattergram)是繪圖中最常見的圖形類型之一闺骚,通常用于顯示和比較數(shù)值序六。散點(diǎn)圖是使用一系列的散點(diǎn)在直角坐標(biāo)系中展示變量的數(shù)值分布琴儿。在二維散點(diǎn)圖中躺酒,可以通過觀察兩個變量的數(shù)據(jù)分析啸箫,發(fā)現(xiàn)兩者的關(guān)系與相關(guān)性馆里。
散點(diǎn)圖可以提供三類關(guān)鍵信息:
(1)變量之間是否存在數(shù)量關(guān)聯(lián)趨勢(正相關(guān)轩勘,負(fù)相關(guān)筒扒,不相關(guān)等);
(2)如果存在關(guān)聯(lián)趨勢绊寻,是線性還是非線性的花墩;
(3)觀察是否有存在離群值悬秉,從而分析這些離群值對建模分析的影響。
scatter函數(shù)
- 函數(shù)定義:
在向量 x 和 y 指定的位置創(chuàng)建一個包含圓形的散點(diǎn)圖冰蘑,該類型的圖形也稱為氣泡圖和泌。
matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=<deprecated parameter>, edgecolors=None, \*, plotnonfinite=False, data=None, \*\*kwargs)
- 常用參數(shù):
- x,y:
x軸 和 y 軸對應(yīng)的數(shù)據(jù)。根據(jù)x和y的值祠肥,在二維平面對應(yīng)位置創(chuàng)建一個包含圓形的散點(diǎn)圖 - s:
散點(diǎn)標(biāo)記面積允跑,以平方磅為單位的標(biāo)記面積√赂蹋可以為標(biāo)量或者等長的數(shù)組。 - c:
散點(diǎn)標(biāo)記的顏色索烹,為指定的色彩工碾、數(shù)值序列或者顏色序列。 - marker:
散點(diǎn)標(biāo)記類型百姓,默認(rèn)為圓圈渊额。 - cmap:
僅當(dāng)c參數(shù)為顏色序列的時候使用。 - aplha:
透明圖設(shè)置垒拢,在顯示多個變量的場景下為了能夠顯示堆疊的散點(diǎn)旬迹,可以設(shè)置該參數(shù)。取值范圍[0求类,1]奔垦。0:透明,1:不透明尸疆。 - norm:
僅當(dāng)c為數(shù)值序列的時候椿猎,通過colors.Normalize將值進(jìn)行正則化。 - linewidths:
散點(diǎn)標(biāo)記的邊界的寬度寿弱。 - edgecolors:
散點(diǎn)標(biāo)記的邊界的顏色犯眠。
scatter的詳細(xì)定義:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.scatter.html?highlight=scatter#matplotlib.pyplot.scatter
示例說明:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
n = 1024
X = np.random.normal(0,1,n)
Y = np.random.normal(0,1,n)
#根據(jù)x,y生成一個數(shù)列,將對應(yīng)的數(shù)據(jù)點(diǎn)映射到coclarmap中的顏色上
C = np.arctan(X/Y)
'''
s:大小固定設(shè)置為64
c:通過arctan函數(shù)隨機(jī)生成一個數(shù)列症革,映射到coloarmap的顏色上
makrer:散點(diǎn)標(biāo)記類型設(shè)置為‘o’對應(yīng)圓圈
aplpha:透明度設(shè)置為0.5筐咧,重合部分可以正常顯示
linewidths:為散點(diǎn)標(biāo)記的邊界寬度
edgecolors:散點(diǎn)邊界的點(diǎn)色,“w”為白色
'''
plt.scatter(X,Y, s=64, c=C, marker='o',alpha=.5,linewidths=2,edgecolors='w')
plt.xlim(-1.5,1.5)
plt.ylim(-1.5,1.5)
# savefig('../figures/scatter_ex.png',dpi=48)
plt.show()
擴(kuò)展應(yīng)用:
- 添加圖列
通過legend_elements()函數(shù)根據(jù)size和color給散點(diǎn)圖增加圖例噪矛。
import numpy as np
import matplotlib.pyplot as plt
N=50
x, y = np.random.rand(2, N)
c = np.random.randint(1, 4, size=N)
s = np.random.randint(10, 100, size=N)
fig, ax = plt.subplots()
scatter = ax.scatter(x, y, c=c, s=s)
# 按照散點(diǎn)圖中標(biāo)記的colors生成legend
legendClass = ax.legend(*scatter.legend_elements(prop="colors"),
loc="lower left", title="classes")
ax.add_artist(legendClass)
# 按照散點(diǎn)圖中標(biāo)記的size生成legend
handles, labels = scatter.legend_elements(prop="sizes", alpha=0.6,num=5)
legendSize = ax.legend(handles, labels, loc="upper right", title="Sizes")
ax.add_artist(legendSize)
plt.show()
- 添加邊界
對于不同別的參數(shù)量蕊,可以通過增加邊界,可以衡量數(shù)據(jù)的離散摩疑,分布等特征危融。
利用scipy中的ConvexHull(凸包)函數(shù),將給定的數(shù)據(jù)按照邊界形成多邊形雷袋,將所有數(shù)據(jù)包含在里面吉殃。凸包是給定平面上一個點(diǎn)集辞居,凸包就是將最外圍的點(diǎn)連接起來構(gòu)成的凸多邊形,它能包含點(diǎn)集中所有的點(diǎn)蛋勺。
import numpy as np
np.random.seed(19680801)
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
fig, ax = plt.subplots()
n = 300
X = np.random.normal(1,1,n)
Y1 = np.random.normal(1,1,n)
X2 = np.random.normal(2.5,1,n)
Y2 = np.random.normal(2.5,2,n)
ax.scatter(X,Y1,c="tab:blue",alpha=0.5,label="blue")
ax.scatter(X2,Y2,c='tab:orange',alpha=0.5,label="orange")
ax.grid(True)
#利用ConvexHull函數(shù)獲得凸包的多邊形點(diǎn)瓦灶,然后利用Polygon來繪制對應(yīng)的邊界
def encircle(x,y, ax=None, **kw):
if not ax: ax=plt.gca()
p = np.c_[x,y]
hull = ConvexHull(p)
poly = plt.Polygon(p[hull.vertices,:], **kw)
ax.add_patch(poly)
#填充色
encircle(X, Y1, ec="g", fc="gold", alpha=0.1)
#邊界線
encircle(X, Y1, ec="firebrick", fc="none", linewidth=1.5)
plt.show()