欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python可視化學(xué)習(xí)之seaborn繪制矩陣圖詳解

 更新時間:2022年02月24日 16:54:50   作者:qq_21478261  
矩陣圖即用一張圖繪制多個變量之間的關(guān)系,數(shù)據(jù)挖掘中常用于初期數(shù)據(jù)探索。本文介紹python中seaborn.pairplot和seaborn.PairGrid繪制矩陣圖,需要的可以參考一下

本文內(nèi)容速覽

1、繪圖數(shù)據(jù)準(zhǔn)備

還是使用鳶尾花iris數(shù)據(jù)集

#導(dǎo)入本帖要用到的庫,聲明如下:
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ù)集簡單統(tǒng)計

2、seaborn.pairplot

語法: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)#文本大小

對角線4張圖是變量自身的分布直方圖;

非對角線的 12 張就是某個變量和另一個變量的關(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、顏色號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)

非對角線散點(diǎn)圖加趨勢線 

import palettable
g = sns.pairplot(pd_iris,
                 hue='class',
                 palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors,
                 kind='reg',#默認(rèn)為scatter,reg加上趨勢線                 
                
                )
sns.set(style='whitegrid')
g.fig.set_size_inches(12,12)
sns.set(style='whitegrid',font_scale=1.5)

對角線上的四個圖繪制方式

可選參數(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),#非對角線上的圖marker大小、外框、外框線寬
                 diag_kws=dict(shade=True)#對角線上核密度圖是否填充
                 
                
                )
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 = sns.PairGrid(pd_iris, 
                 hue='class',
                 palette='husl',)
g = g.map(plt.scatter)#map每個子圖繪制一樣類型的圖
g = g.add_legend()
g.fig.set_size_inches(12,12)
sns.set(style='whitegrid',font_scale=1.5)

對角線和非對角線分別繪制不同類型圖

g = sns.PairGrid(pd_iris, 
                 hue='class',
                palette='Set1',)
g = g.map_diag(plt.hist)#對角線繪制直方圖
g = g.map_offdiag(plt.scatter)#非對角線繪制散點(diǎn)圖
g = g.add_legend()
g.fig.set_size_inches(12,12)
sns.set(style='whitegrid',font_scale=1.5)

對角線上方、對角線、對角線下方分別繪制不同類型圖

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)#對角圖形顏色
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矩陣圖的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 詳解Django中的權(quán)限和組以及消息

    詳解Django中的權(quán)限和組以及消息

    這篇文章主要介紹了詳解Django中的權(quán)限和組以及消息,在Python百花齊放的web框架中,Django是人氣最高的一個,需要的朋友可以參考下
    2015-07-07
  • 詳解Python中range()與xrange()的區(qū)別

    詳解Python中range()與xrange()的區(qū)別

    range()?和?xrange()?是兩個函數(shù),可用于在?Python的?for?循環(huán)中迭代一定次數(shù)。本文將通過示例詳細(xì)說說二者的區(qū)別與使用,需要的可以參考一下
    2022-09-09
  • Python中的Networkx的基本使用

    Python中的Networkx的基本使用

    Networkx是一個Python的包,可以用來創(chuàng)建和處理復(fù)雜的圖網(wǎng)絡(luò)結(jié)構(gòu),這篇文章主要介紹了Python中的Networkx詳解,需要的朋友可以參考下
    2023-02-02
  • 一文讀懂Python 枚舉

    一文讀懂Python 枚舉

    這篇文章主要介紹了Python 枚舉的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-08-08
  • 詳解Pandas的三大利器(map,apply,applymap)

    詳解Pandas的三大利器(map,apply,applymap)

    這篇文章主要為大家介紹了pandas中的三大利器: map、apply、applymap,他們經(jīng)常在進(jìn)行數(shù)據(jù)處理的時候用到,需要的可以參考一下
    2022-02-02
  • pytorch LayerNorm參數(shù)的用法及計算過程

    pytorch LayerNorm參數(shù)的用法及計算過程

    這篇文章主要介紹了pytorch LayerNorm參數(shù)的用法及計算過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python?Pandas條件篩選功能

    Python?Pandas條件篩選功能

    這篇文章主要介紹了Python?Pandas條件篩選功能,篩選是在平時的工作中使用非常頻繁的功能了,下文詳細(xì)的相關(guān)資料介紹,需要的小伙伴可以參考一下
    2022-03-03
  • Python中你應(yīng)該知道的一些內(nèi)置函數(shù)

    Python中你應(yīng)該知道的一些內(nèi)置函數(shù)

    python提供了內(nèi)聯(lián)模塊buidin,該模塊定義了一些軟件開發(fā)中常用的函數(shù),這些函數(shù)實(shí)現(xiàn)了數(shù)據(jù)類型的轉(zhuǎn)換,數(shù)據(jù)的計算,序列的處理等功能。下面這篇文章主要給大家介紹了Python中一些大家應(yīng)該知道的內(nèi)置函數(shù),文中總結(jié)的非常詳細(xì),需要的朋友們下面來一起看看吧。
    2017-03-03
  • python構(gòu)建指數(shù)平滑預(yù)測模型示例

    python構(gòu)建指數(shù)平滑預(yù)測模型示例

    今天小編就為大家分享一篇python構(gòu)建指數(shù)平滑預(yù)測模型示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python讀取圖片EXIF信息類庫介紹和使用實(shí)例

    Python讀取圖片EXIF信息類庫介紹和使用實(shí)例

    這篇文章主要介紹了Python讀取圖片EXIF信息類庫介紹和使用實(shí)例,例如Python Imaging Library、EXIF.py等,需要的朋友可以參考下
    2014-07-07

最新評論