日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python 树状图可视化_Python可视化25|seaborn矩阵图

發(fā)布時間:2024/9/19 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 树状图可视化_Python可视化25|seaborn矩阵图 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

矩陣圖即用一張圖繪制多個變量之間的關(guān)系,數(shù)據(jù)挖掘中常用于初期數(shù)據(jù)探索;
本文介紹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)系。

  • 加上分類變量
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調(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)

  • 非對角線散點圖加趨勢線
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ù)為‘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)格下三角圖形
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,'.','+'],#散點圖的markerplot_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)

  • 其它一些參數(shù)修改
g = sns.PairGrid(pd_iris, hue='class',palette='Set1',hue_kws={"marker": ["^", "s", "D"]},#設(shè)置markerdiag_sharey=False,) g = g.map_upper(sns.scatterplot,edgecolor="w", s=40)#設(shè)置點大小,外框顏色 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)

參考資料:

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。