Python可視化學(xué)習(xí)之seaborn繪制矩陣圖詳解
本文內(nèi)容速覽


1、繪圖數(shù)據(jù)準(zhǔn)備
還是使用鳶尾花iris數(shù)據(jù)集
#導(dǎo)入本帖要用到的庫(kù),聲明如下:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
from sklearn import datasets
import seaborn as sns
#導(dǎo)入鳶尾花iris數(shù)據(jù)集(方法一)
#該方法更有助于理解數(shù)據(jù)集
iris=datasets.load_iris()
x, y =iris.data,iris.target
y_1 = np.array(['setosa' if i==0 else 'versicolor' if i==1 else 'virginica' for i in y])
pd_iris = pd.DataFrame(np.hstack((x, y_1.reshape(150,1))),columns=['sepal length(cm)','sepal width(cm)','petal length(cm)','petal width(cm)','class'])
#astype修改pd_iris中數(shù)據(jù)類型object為float64
pd_iris['sepal length(cm)']=pd_iris['sepal length(cm)'].astype('float64')
pd_iris['sepal width(cm)']=pd_iris['sepal width(cm)'].astype('float64')
pd_iris['petal length(cm)']=pd_iris['petal length(cm)'].astype('float64')
pd_iris['petal width(cm)']=pd_iris['petal width(cm)'].astype('float64')
#導(dǎo)入鳶尾花iris數(shù)據(jù)集(方法二)
#import seaborn as sns
#iris_sns = sns.load_dataset("iris")
數(shù)據(jù)集簡(jiǎn)單統(tǒng)計(jì)

2、seaborn.pairplot
語(yǔ)法:seaborn.pairplot(data, hue=None, hue_order=None, palette=None, vars=None, x_vars=None, y_vars=None, kind='scatter', diag_kind='auto', markers=None, height=2.5, aspect=1, corner=False, dropna=True, plot_kws=None, diag_kws=None, grid_kws=None, size=None)
g = sns.pairplot(pd_iris) g.fig.set_size_inches(12,12)#figure大小 sns.set(style='whitegrid',font_scale=1.5)#文本大小

對(duì)角線4張圖是變量自身的分布直方圖;
非對(duì)角線的 12 張就是某個(gè)變量和另一個(gè)變量的關(guān)系。
加上分類變量
g = sns.pairplot(pd_iris,
hue='class'#按照三種花分類
)
sns.set(style='whitegrid')
g.fig.set_size_inches(12,12)
sns.set(style='whitegrid',font_scale=1.5)

修改調(diào)色盤
可以使用Matplotlib、seaborn、顏色號(hào)list等色盤。
可參考:Python可視化學(xué)習(xí)之seaborn調(diào)色盤
import palettable
g = sns.pairplot(pd_iris,
hue='class',
palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors,#palettable顏色盤
)
sns.set(style='whitegrid')
g.fig.set_size_inches(12,12)
sns.set(style='whitegrid',font_scale=1.5)
g = sns.pairplot(pd_iris,
hue='class',
palette='Set1',#Matplotlib顏色
)
sns.set(style='whitegrid')
g.fig.set_size_inches(12,12)
sns.set(style='whitegrid',font_scale=1.5)
g = sns.pairplot(pd_iris,
hue='class',
palette=['#dc2624', '#2b4750', '#45a0a2'],#使用傳入的顏色list
)
sns.set(style='whitegrid')
g.fig.set_size_inches(12,12)
sns.set(style='whitegrid',font_scale=1.5)
x,y軸方向選取相同子集
import palettable
g = sns.pairplot(pd_iris,
hue='class',
palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors,
vars=['sepal length(cm)','sepal width(cm)'],#x,y軸方向選取相同子集繪圖
)
sns.set(style='whitegrid')
g.fig.set_size_inches(12,6)
sns.set(style='whitegrid',font_scale=1.5)
x,y軸方向選取不同子集
import palettable
g = sns.pairplot(pd_iris,
hue='class',
palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors,
x_vars=['sepal length(cm)','sepal width(cm)'],#x,y軸方向選取不同子集
y_vars=['petal length(cm)','petal width(cm)'],
)
sns.set(style='whitegrid')
g.fig.set_size_inches(12,6)
sns.set(style='whitegrid',font_scale=1.5)
非對(duì)角線散點(diǎn)圖加趨勢(shì)線
import palettable
g = sns.pairplot(pd_iris,
hue='class',
palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors,
kind='reg',#默認(rèn)為scatter,reg加上趨勢(shì)線
)
sns.set(style='whitegrid')
g.fig.set_size_inches(12,12)
sns.set(style='whitegrid',font_scale=1.5)
對(duì)角線上的四個(gè)圖繪制方式
可選參數(shù)為‘auto’, ‘hist’(默認(rèn)), ‘kde’, None。
import palettable
g = sns.pairplot(pd_iris,
hue='class',
palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors,
diag_kind='hist',#hist直方圖
)
sns.set(style='whitegrid')
g.fig.set_size_inches(12,12)
sns.set(style='whitegrid',font_scale=1.5)
只顯示網(wǎng)格下三角圖形
import palettable
g = sns.pairplot(pd_iris,
hue='class',
palette='Set1',
corner=True#圖形顯示左下角
)
g.fig.set_size_inches(12,12)
sns.set(font_scale=1.5)
圖形外觀設(shè)置
import palettable
g = sns.pairplot(pd_iris,
hue='class',
palette='Set1',
markers=['$\clubsuit$','.','+'],#散點(diǎn)圖的marker
plot_kws=dict(s=50, edgecolor="r", linewidth=1),#非對(duì)角線上的圖marker大小、外框、外框線寬
diag_kws=dict(shade=True)#對(duì)角線上核密度圖是否填充
)
g.fig.set_size_inches(12,12)
sns.set(font_scale=1.5)
3、seaborn.PairGrid(更靈活的繪制矩陣圖)
seaborn.PairGrid(data, hue=None, hue_order=None, palette=None, hue_kws=None, vars=None, x_vars=None, y_vars=None, corner=False, diag_sharey=True, height=2.5, aspect=1, layout_pad=0, despine=True, dropna=True, size=None)
每個(gè)子圖繪制同類型的圖
g = sns.PairGrid(pd_iris,
hue='class',
palette='husl',)
g = g.map(plt.scatter)#map每個(gè)子圖繪制一樣類型的圖
g = g.add_legend()
g.fig.set_size_inches(12,12)
sns.set(style='whitegrid',font_scale=1.5)
對(duì)角線和非對(duì)角線分別繪制不同類型圖
g = sns.PairGrid(pd_iris,
hue='class',
palette='Set1',)
g = g.map_diag(plt.hist)#對(duì)角線繪制直方圖
g = g.map_offdiag(plt.scatter)#非對(duì)角線繪制散點(diǎn)圖
g = g.add_legend()
g.fig.set_size_inches(12,12)
sns.set(style='whitegrid',font_scale=1.5)
對(duì)角線上方、對(duì)角線、對(duì)角線下方分別繪制不同類型圖
g = sns.PairGrid(pd_iris, hue='class',) g = g.map_upper(sns.scatterplot) g = g.map_lower(sns.kdeplot, colors="C0") g = g.map_diag(sns.kdeplot, lw=2)3繪制核密度圖 g = g.add_legend()#添加圖例 sns.set(style='whitegrid',font_scale=1.5)

其它一些參數(shù)修改
g = sns.PairGrid(pd_iris, hue='class',
palette='Set1',
hue_kws={"marker": ["^", "s", "D"]},#設(shè)置marker
diag_sharey=False,
)
g = g.map_upper(sns.scatterplot,edgecolor="w", s=40)#設(shè)置點(diǎn)大小,外框顏色
g = g.map_lower(sns.kdeplot, colors="#01a2d9")#設(shè)置下三角圖形顏色
g = g.map_diag(sns.kdeplot, lw=3)#對(duì)角圖形顏色
g = g.add_legend()#添加圖例
g.fig.set_size_inches(12,12)
sns.set(style='whitegrid',font_scale=1.5)

以上就是Python可視化學(xué)習(xí)之seaborn繪制矩陣圖詳解的詳細(xì)內(nèi)容,更多關(guān)于Python seaborn矩陣圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Python中range()與xrange()的區(qū)別
range()?和?xrange()?是兩個(gè)函數(shù),可用于在?Python的?for?循環(huán)中迭代一定次數(shù)。本文將通過(guò)示例詳細(xì)說(shuō)說(shuō)二者的區(qū)別與使用,需要的可以參考一下2022-09-09
詳解Pandas的三大利器(map,apply,applymap)
這篇文章主要為大家介紹了pandas中的三大利器: map、apply、applymap,他們經(jīng)常在進(jìn)行數(shù)據(jù)處理的時(shí)候用到,需要的可以參考一下2022-02-02
pytorch LayerNorm參數(shù)的用法及計(jì)算過(guò)程
這篇文章主要介紹了pytorch LayerNorm參數(shù)的用法及計(jì)算過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
Python中你應(yīng)該知道的一些內(nèi)置函數(shù)
python提供了內(nèi)聯(lián)模塊buidin,該模塊定義了一些軟件開發(fā)中常用的函數(shù),這些函數(shù)實(shí)現(xiàn)了數(shù)據(jù)類型的轉(zhuǎn)換,數(shù)據(jù)的計(jì)算,序列的處理等功能。下面這篇文章主要給大家介紹了Python中一些大家應(yīng)該知道的內(nèi)置函數(shù),文中總結(jié)的非常詳細(xì),需要的朋友們下面來(lái)一起看看吧。2017-03-03
python構(gòu)建指數(shù)平滑預(yù)測(cè)模型示例
今天小編就為大家分享一篇python構(gòu)建指數(shù)平滑預(yù)測(cè)模型示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Python讀取圖片EXIF信息類庫(kù)介紹和使用實(shí)例
這篇文章主要介紹了Python讀取圖片EXIF信息類庫(kù)介紹和使用實(shí)例,例如Python Imaging Library、EXIF.py等,需要的朋友可以參考下2014-07-07

