一、分布資料可視化 - 散點圖 jointplot() / pairplot() 加載子產品 import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats as sci
1、綜合散點圖 - jointplot() 1.1散點圖 + 分布圖 示例1: #建立資料
rs = np.random.RandomState(2)
df = pd.DataFrame(rs.randn(200,2), columns = ['A','B'])
sns.jointplot(x=df['A'], y=df['B'], #設定xy軸,顯示columns名稱
data = df, #設定資料
color = 'b', #設定顔色
s = 50, edgecolor = 'w', linewidth = 1,#設定散點大小、邊緣顔色及寬度(隻針對scatter)
stat_func=sci.pearsonr,
kind = 'scatter',#設定類型:'scatter','reg','resid','kde','hex'
#stat_func=<function pearsonr>,
space = 0.1, #設定散點圖和布局圖的間距
size = 8, #圖表大小(自動調整為正方形))
ratio = 5, #散點圖與布局圖高度比,整型
marginal_kws = dict(bins=15, rug =True), #設定柱狀圖箱數,是否設定rug
)
python可視化進階---seaborn1.4 分布資料可視化 - 散點圖 jointplot() / pairplot() 圖1.散點分布圖 注意:如果不顯示r值(pearsonr),可以在參數中添加stat_func=sci.pearsonr,有就不用添加了 示例2:六邊形圖 sns.jointplot(x=df['A'], y=df['B'], #設定xy軸,顯示columns名稱
data = df, #設定資料
color = 'b', #設定顔色
#s = 50, edgecolor = 'w', linewidth = 1,#設定散點大小、邊緣顔色及寬度(隻針對scatter)
stat_func=sci.pearsonr,
kind = 'hex',#設定類型:'scatter','reg','resid','kde','hex'
space = 0.1, #設定散點圖和布局圖的間距
size = 8, #圖表大小(自動調整為正方形))
ratio = 5, #散點圖與布局圖高度比,整型
# marginal_kws = dict(bins=15, rug =True) #設定柱狀圖箱數,是否設定rug
)
python可視化進階---seaborn1.4 分布資料可視化 - 散點圖 jointplot() / pairplot() 六邊形圖也可以叫做蜂巢圖 示例3:六邊形圖 #建立資料
df = pd.DataFrame(rs.randn(500,2), columns = ['A', 'B'])
with sns.axes_style('white'):
sns.jointplot(x=df['A'], y=df['B'], data = df, kind = 'hex',
color = 'k',stat_func=sci.pearsonr,
marginal_kws = dict(bins = 20))
python可視化進階---seaborn1.4 分布資料可視化 - 散點圖 jointplot() / pairplot() 示例4: 密度圖 #建立資料
rs = np.random.RandomState(15)
df = pd.DataFrame(rs.randn(300,2), columns = ['A', 'B'])
#建立密度圖
g = sns.jointplot(x = df['A'], y = df['B'], data = df,
kind = 'kde', color = 'k', stat_func= sci.pearsonr,
shade_lowest = False)
#添加散點圖
g.plot_joint(plt.scatter, c = 'w', s = 30, linewidth = 1, marker='+')
python可視化進階---seaborn1.4 分布資料可視化 - 散點圖 jointplot() / pairplot() 1.2可拆分繪制的散點圖 #plot_joint() + ax_marg_x.hist() + ax_marg_y.hist() 示例1:拆分圖 #設定風格
sns.set_style('white')
#導入資料
tips = sns.load_dataset('tips')
print(tips.head())
#建立一個繪圖表格區域,設定好x,y對應資料
g = sns.JointGrid(x = 'total_bill', y = 'tip', data = tips)
g.plot_joint(plt.scatter, color = 'm', edgecolor = 'white') #設定框内圖表,scatter
g.ax_marg_x.hist(tips['total_bill'], color='b', alpha = .6,
bins = np.arange(0,60,3)) #設定x軸為直方圖,注意bins是數組
g.ax_marg_y.hist(tips['tip'], color = 'r', alpha = .6,
orientation = 'horizontal',
bins = np.arange(0,12,1)) #設定x軸直方圖,注意需要orientation參數
from scipy import stats
g.annotate(stats.pearsonr)
#設定标注,可以為pearsonar, spearmanr
plt.grid(linestyle = '--')
python可視化進階---seaborn1.4 分布資料可視化 - 散點圖 jointplot() / pairplot() 示例2:兩個條形圖在一個函數裡進行設定 #建立一個繪圖表格區域,設定好x,y對應資料
g = sns.JointGrid(x = 'total_bill', y = 'tip', data = tips)
g = g.plot_joint(plt.scatter, color = 'g', s = 40, edgecolor = 'white') #繪制散點圖
plt.grid(linestyle = '--')
g.plot_marginals(sns.distplot, kde = True, color = 'g') #繪制x,y直方圖
python可視化進階---seaborn1.4 分布資料可視化 - 散點圖 jointplot() / pairplot() 示例3:kde - 密度圖 #建立一個繪圖表格區域,設定好x,y對應資料
g = sns.JointGrid(x = 'total_bill', y = 'tip', data = tips)
g = g.plot_joint(sns.kdeplot, cmap = 'Reds_r') #繪制密度圖
plt.grid(linestyle = '--')
g.plot_marginals(sns.kdeplot, shade = True, color = 'r') #繪制x,y軸密度圖
python可視化進階---seaborn1.4 分布資料可視化 - 散點圖 jointplot() / pairplot() 2.矩陣散點圖 - pairplot() 示例1:普通矩陣圖示意 #設定風格
sns.set_style('white')
#讀取資料
iris = sns.load_dataset('iris')
print(iris.head())
sns.pairplot(iris,
kind = 'scatter', #散點圖/回歸分布圖{'scatter', 'reg'})
diag_kind = 'hist', #直方圖/密度圖{'hist', 'kde'}
hue = 'species', #按照某一字段進行分類
palette = 'husl', #設定調色闆
markers = ['o', 's', 'D'], #設定不同系列的點樣式(這裡根據參考分類個數)
size = 2 #圖示大小
)
python可視化進階---seaborn1.4 分布資料可視化 - 散點圖 jointplot() / pairplot() 示例2:隻提取局部變量進行對比 g = sns.pairplot(iris, vars = ['sepal_width', 'sepal_length'],
kind = 'reg', diag_kind = 'kde',
hue = 'species', palette = 'husl')
python可視化進階---seaborn1.4 分布資料可視化 - 散點圖 jointplot() / pairplot() 示例3:其它參數設定 #其它參數設定
sns.pairplot(iris, diag_kind = 'kde', markers = '+',
plot_kws = dict(s = 50, edgecolor = 'b', linewidth = 1),
#設定點樣式
diag_kws = dict(shade = True)
)#設定密度圖樣式
python可視化進階---seaborn1.4 分布資料可視化 - 散點圖 jointplot() / pairplot() 示例4:可拆分繪制的散點圖 # map_diag() + map_offdiag()
g = sns.PairGrid(iris, hue= 'species', palette = 'hls',
vars = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width'])
#可篩選建立一個繪圖表格區域,設定好x,y對應的資料,按照species分類
#對角線圖表,plt.hist/sns.kdeplot
g.map_diag(plt.hist,
histtype = 'step', #可選:'bar','barstacked', 'step', 'stepfilled'
linewidth = 1, edgecolor = 'w')
#其它圖表:plt.scatter/plt.bar...
g.map_offdiag(plt.scatter, edgecolor = 'w', s = 40, linewidth = 1)
#設定點顔色、大小、描邊寬度
g.add_legend() #添加圖例()
python可視化進階---seaborn1.4 分布資料可視化 - 散點圖 jointplot() / pairplot() 示例5:上三角和下三角#map_diag() + map_lower() + map_upper() g = sns.PairGrid(iris)
g.map_diag(sns.kdeplot, lw=3) #設定對角線圖表
g.map_upper(plt.scatter, color = 'r') #設定對角線上端圖表
g.map_lower(sns.kdeplot, cmap='Blues_d') #設定對角線下端圖表
python可視化進階---seaborn1.4 分布資料可視化 - 散點圖 jointplot() / pairplot()