matplotlib可視化之Scatter散點(diǎn)圖

散點(diǎn)圖

Scatter chart(PointGraph, X-Y Plot, Scatter Chart或者 Scattergram)是繪圖中最常見的圖形類型之一,通常用于顯示和比較數(shù)值。散點(diǎn)圖是使用一系列的散點(diǎn)在直角坐標(biāo)系中展示變量的數(shù)值分布。在二維散點(diǎn)圖中,可以通過觀察兩個(gè)變量的數(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)觀察是否有存在離群值,從而分析這些離群值對(duì)建模分析的影響。

scatter函數(shù)

  1. 函數(shù)定義:
    在向量 x 和 y 指定的位置創(chuàng)建一個(gè)包含圓形的散點(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)
  1. 常用參數(shù):
  • x,y:
    x軸 和 y 軸對(duì)應(yīng)的數(shù)據(jù)。根據(jù)x和y的值,在二維平面對(duì)應(yīng)位置創(chuàng)建一個(gè)包含圓形的散點(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ù)為顏色序列的時(shí)候使用。
  • aplha:
    透明圖設(shè)置,在顯示多個(gè)變量的場景下為了能夠顯示堆疊的散點(diǎn),可以設(shè)置該參數(shù)。取值范圍[0,1]。0:透明,1:不透明。
  • norm:
    僅當(dāng)c為數(shù)值序列的時(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生成一個(gè)數(shù)列,將對(duì)應(yīng)的數(shù)據(jù)點(diǎn)映射到coclarmap中的顏色上
C = np.arctan(X/Y)
'''
s:大小固定設(shè)置為64
c:通過arctan函數(shù)隨機(jī)生成一個(gè)數(shù)列,映射到coloarmap的顏色上
makrer:散點(diǎn)標(biāo)記類型設(shè)置為‘o’對(duì)應(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()
  • 添加邊界
    對(duì)于不同別的參數(shù),可以通過增加邊界,可以衡量數(shù)據(jù)的離散,分布等特征。
    利用scipy中的ConvexHull(凸包)函數(shù),將給定的數(shù)據(jù)按照邊界形成多邊形,將所有數(shù)據(jù)包含在里面。凸包是給定平面上一個(gè)點(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來繪制對(duì)應(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()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容