python 树状图可视化_Python可视化25|seaborn矩阵图
生活随笔
收集整理的這篇文章主要介紹了
python 树状图可视化_Python可视化25|seaborn矩阵图
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
矩陣圖即用一張圖繪制多個變量之間的關(guān)系,數(shù)據(jù)挖掘中常用于初期數(shù)據(jù)探索;
本文介紹python中seaborn.pairplot(傻瓜版)和seaborn.PairGrid(更個性化版)繪制矩陣圖
本文介紹python中seaborn.pairplot(傻瓜版)和seaborn.PairGrid(更個性化版)繪制矩陣圖
本文內(nèi)容速覽
歡迎隨緣關(guān)注@pythonic生物人
目錄
1、繪圖數(shù)據(jù)準(zhǔn)備 2、seaborn.pairplot加上分類變量修改調(diào)色盤x,y軸方向選取相同子集 x,y軸方向選取不同子集非對角線散點圖加趨勢線 對角線上的四個圖繪制方式只顯示網(wǎng)格下三角圖形 圖形外觀設(shè)置 3、seaborn.PairGrid(更靈活的繪制矩陣圖)每個子圖繪制同類型的圖對角線和非對角線分別繪制不同類型圖對角線上方、對角線、對角線下方分別繪制不同類型圖其它一些參數(shù)修改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)系。
- 加上分類變量
- 修改調(diào)色盤
可以使用Matplotlib、seaborn、顏色號list等色盤。可參考:python調(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軸方向選取相同子集
- x,y軸方向選取不同子集
- 非對角線散點圖加趨勢線
- 對角線上的四個圖繪制方式
可選參數(shù)為‘a(chǎn)uto’, ‘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)格下三角圖形
- 圖形外觀設(shè)置
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)- 每個子圖繪制同類型的圖
- 對角線和非對角線分別繪制不同類型圖
- 對角線上方、對角線、對角線下方分別繪制不同類型圖
- 其它一些參數(shù)修改
參考資料:
http://seaborn.pydata.org/generated/seaborn.pairplot.html#seaborn.pairplothttp://seaborn.pydata.org/generated/seaborn.PairGrid.html#seaborn.PairGrid本文結(jié)束,歡迎隨緣關(guān)注@pythonic生物人
總結(jié)
以上是生活随笔為你收集整理的python 树状图可视化_Python可视化25|seaborn矩阵图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无界限浏览器是什么?
- 下一篇: python程序设计基础实战教程_Pyt