Python可視化25|seaborn繪製矩陣圖

2021-02-21 pythonic生物人
矩陣圖即用一張圖繪製多個變量之間的關係,數據挖掘中常用於初期數據探索;本文介紹python中seaborn.pairplot(傻瓜版)和seaborn.PairGrid(更個性化版)繪製矩陣圖本文內容速覽

目錄
1、繪圖數據準備
2、seaborn.pairplot
   加上分類變量
   修改調色盤
   x,y軸方向選取相同子集 
   x,y軸方向選取不同子集
   非對角線散點圖加趨勢線 
   對角線上的四個圖繪製方式
   只顯示網格下三角圖形 
   圖形外觀設置 
3、seaborn.PairGrid(更靈活的繪製矩陣圖)
   每個子圖繪製同類型的圖
   對角線和非對角線分別繪製不同類型圖
   對角線上方、對角線、對角線下方分別繪製不同類型圖
   其它一些參數修改

1、繪圖數據準備

還是使用鳶尾花iris數據集

#導入本帖要用到的庫,聲明如下:
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

#導入鳶尾花iris數據集(方法一)
#該方法更有助於理解數據集
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中數據類型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')


#導入鳶尾花iris數據集(方法二)
#import seaborn as sns
#iris_sns = sns.load_dataset("iris")

數據集簡單統計

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 張就是某個變量和另一個變量的關係。

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)

可以使用Matplotlib、seaborn、顏色號list等色盤。可參考:python調色盤

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)

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)

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)

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

可選參數為『auto』, 『hist』(默認), 『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)

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)

import palettable
g = sns.pairplot(pd_iris,
                 hue='class',
                 palette='Set1',
                 markers=['$\clubsuit,'.','+'],#散點圖的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)#非對角線繪製散點圖
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)

g = sns.PairGrid(pd_iris, hue='class',
                 palette='Set1',
                 hue_kws={"marker": ["^", "s", "D"]},#設置marker
                 diag_sharey=False,
                )
g = g.map_upper(sns.scatterplot,edgecolor="w", s=40)#設置點大小,外框顏色
g = g.map_lower(sns.kdeplot, colors="#01a2d9")#設置下三角圖形顏色
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)

參考資料:

http://seaborn.pydata.org/generated/seaborn.pairplot.html#seaborn.pairplothttp://seaborn.pydata.org/generated/seaborn.PairGrid.html#seaborn.PairGrid

相關焦點

  • Python簡單高效的可視化神器——Seaborn
    如何用python畫圖——帶你入門matplotlib如何用python畫圖--matplotlib實例與補充我們也在文章Python可視化工具概覽中介紹了,seaborn其實是在matplotlib基礎上進行了更高級的封裝,使得一些出圖更加的快捷方便而且美觀。
  • 數據可視化 | seaborn繪製散點圖
    Python-seaborn 繪製多類別散點圖由於涉及的圖表類型為多類別散點圖的繪製,在使用常規matplotlib進行繪製時會顯得格外繁瑣,所以我們選擇了對matplotlib進行了更高級的API封裝,使作圖更加容易的seaborn包進行圖表的繪製,更多seaborn 介紹,大家可以直接去seaborn官網
  • 用數據可視化工具Seaborn繪製圖表
    使用seaborn和Pokemon(口袋妖怪)數據集的屬性,我們將創建一些非常有趣的可視化效果。我們首先要看的是散點圖。散點圖散點圖使用點來表示不同數值變量的值。每個點在水平軸和垂直軸上的位置表示單個數據點的值。它們用於觀察變量之間的關係。
  • python可視化之seaborn——熱力圖
    linewidths=0, #定義熱力圖裡「表示兩兩特徵關係的矩陣小塊」之間的間隔大小        linecolor=』white』, #切分熱力圖上每個矩陣小塊的線的顏色,默認值是』white』        cbar=True, #是否在熱力圖側邊繪製顏色刻度條
  • Python中得可視化:使用Seaborn繪製常用圖表
    可視化是Seaborn的核心部分,可以幫助探索和理解數據。此圖是機器學習領域的最強大的可視化工具。 讓我們看看數據集評級和大小中的兩個數字列的散點圖是什麼樣子的。首先,我們將使用matplotlib繪製圖,然後我們將看到它在seaborn中的樣子。
  • Python數據可視化-seaborn Iris鳶尾花數據
    StripplotStripplot的本質就是把數據集中具有quantitative屬性的變量按照類別去做散點圖(Scatterplot)。我們將紙鳶花數據集中不同種類花的sepal length做stripplot可視化上邊左側的圖片便是在默認風格下用stripplot繪製的散點圖。
  • Python-seaborn 基礎圖表繪製-散點圖
    作者:寧海濤 來源:DataCharm上期推文推出第一篇基礎圖表繪製-R-ggplot2 基礎圖表繪製-散點圖 的繪製推文,得到了很多小夥伴的喜歡,也是我更加想使這個系列做的更加完善和系統,我之前也有說過,會推出Python和R的兩個版本繪製教程,接下來我們就推出基礎散點圖的Python繪製版本。
  • Python可視化26|seaborn繪製分面圖(seaborn.FacetGrid)
    本文介紹seaborn繪製分面圖,即將一個數據集按某類,分行或分列顯示為多個子圖;前面文章介紹的seaborn.catplot、seaborn.pairplot、seaborn.lmplot生成分面圖的底層就是本文的主角seaborn.FacetGrid。
  • Python 數據可視化之 seaborn 使用詳解
    python中的一個非常強大的數據可視化庫,它集成了matplotlib,下圖為seaborn的官網,如果遇到疑惑的地方可以到官網查看。http://seaborn.pydata.org/從官網的主頁我們就可以看出,seaborn在數據可視化上真的非常強大。1. 首先我們還是需要先引入庫,不過這次要用到的python庫比較多。
  • Python-seaborn 基礎圖表繪製-柱形圖(數據分享)
    上期介紹了使用R-ggplot繪製基礎柱形圖的繪製推文,本期按照慣例,我們繼續推出Python 版本的繪製方法,當然我們也是經過美化修飾的結果,畢竟要自己看的過去才行。本期推文主要涉及的知識點如下:Matplotlib inset_locator.inset_axes()自由添加圖片元素Python-seaborn繪製統計直方圖在使用基礎的matplotlib雖然也能繪製出直方統計圖,但面對多類別數據則顯得較為蠻煩,基本系列課程的目的是為了大家系統掌握各種圖表的繪製方法
  • Python數據可視化:用Seaborn繪製高端玩家版散點圖
    今天我們畫普通散點圖、邊際分布線性回歸散點圖、散點圖矩陣、帶線性回歸最佳擬合線的散點圖。本文示例多是來自官方文檔,這裡我只是做一下整理,讓大家知道散點圖的不同玩法,不要再繪製老掉牙的普通玩家版散點圖了。
  • 嗨客案例 | Seaborn數據可視化
    Michael Waskom所開發的Seaborn提供了一個高層次的界面來繪製更吸引人統計圖形,Seaborn 提供了一個可以快速探索分析數據不同特徵的API接口,繪圖函數通過調用儘量少的參數來實現可視化的過程,此外還可以通過修改附加參數的形式來自定義選項。
  • 【Python教程】用Python進行數據可視化
    在本文中,我將介紹如何開始使用Python和matplotlib、seaborn兩個庫對數據進行可視化。Seaborn 基於 matplotlib,具有多種特性,比如內置主題、調色板、可以可視化單變量數據、雙變量數據,線性回歸數據和數據矩陣以及統計型時序數據等,能讓我們創建複雜的可視化圖形。
  • 【Python可視化6】Seaborn之heatmap熱力圖
    Seaborn是基於matplotlib的Python可
  • 十分鐘掌握Seaborn,進階Python數據可視化分析
    那麼現在開始,十分鐘的時間,你就可以了解 Seaborn 中常用圖形的繪製方法,以及進階的可視化分析技巧。- Seaborn 繪圖上手 -如果你還沒有安裝 Python 環境,那麼推薦你安裝 Anaconda,對於上手 Python 來說更加簡單,不容易出差錯。
  • Seaborn:一行代碼生成酷炫狂拽的數據集可視化
    別人酷炫狂拽,坐標軸上還有直方圖的可視化究竟是怎麼弄的?今天碰到了Seaborn的庫,一行代碼就出圖,愛了!Seaborn介紹Seaborn是Python的數據統計圖形庫。它基於matplotlib構建,並與pandas數據結構緊密集成。Seaborn功能簡介
  • 輕鬆用 Seaborn 進行數據可視化
    sns.set()1 直方圖 (Distplot)sns.distplot()結合直方圖並繪製核密度估計圖。圖4上面顯示的圖表稱為輪廓圖。 輪廓圖(有時稱為「水平圖」)是一種在二維平面上顯示三維表面的方法。 它繪製了y軸上的兩個預測變量X Y和輪廓的響應變量Z.
  • Python可視化 | 日曆圖與熱力圖的可視化教程
    本文以2019年全國各城市的空氣品質觀測數據為例,利用matplotlib、calmap、pyecharts繪製日曆圖和熱力圖。在繪圖之前先利用pandas對空氣品質數據進行處理。數據處理如果要繪製全年的日曆圖或者熱圖,首先要將所有的數據進行合併處理。
  • Python-matplotlib: 散點圖的繪製
    本文涉及的數據主要包括兩種,一種為全球各大洲的網格數據,用於繪製另類散點圖例,一種為全球各州的教育水平的師生比例,用於散點圖的繪圖。各大洲的網格數據如下(部分):如紅框所示,為所需要的數據,用於繪圖。本文的可視化繪製過程涉及seaborn的stripplot()方法,所需的庫、總體設置及用於繪製「抖動」的散點圖(類似ggplot2的position_jitter()),其目的就是為了防止散點重疊。
  • 14個Seaborn數據可視化圖
    Facet Grid回歸圖簡介 Seaborn是一個基於matplotlib的Python數據可視化庫。它提供了一個高級界面,以繪製曲線和信息統計圖形。圖11:『年齡』與『P-class』之間的swarm圖矩陣圖 這些是使用二維矩陣數據進行可視化的特殊類型的圖形。