本文介紹兩個變量之間的分布圖(Draw a plot of two variables with bivariate and univariate graphs.)。
本文内容速看
- seaborn.jointplot繪制兩個變量分布圖
- seaborn.JointGrid繪制兩個變量分布圖(更個性化)
目錄
1、繪圖資料準備
2、seaborn.jointplot
圖形基本設定
修改中部圖
修改邊際圖
kind參數設定
給中部散點圖加上核密度圖
3、seaborn.JointGrid
sns.JointGrid基礎使用
分别繪制中心及邊際圖
分别個性化繪制邊際圖
分别繪制x軸方向及y軸方向邊際圖
1、繪圖資料準備
還是鸢尾花iris資料集:鸢尾花iris資料集介紹
#導入本帖要用到的庫,聲明如下:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import palettable
from pandas import Series,DataFrame
from sklearn import datasets
import seaborn as sns
import palettable
#導入鸢尾花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.jointplot
seaborn.jointplot(x, y, data=None, kind='scatter', stat_func=None, color=None, height=6, ratio=5, space=0.2, dropna=True, xlim=None, ylim=None, joint_kws=None, marginal_kws=None, annot_kws=None, **kwargs)
同時繪制兩個變量之間的分布圖(Draw a plot of two variables with bivariate and univariate graphs.)
該圖由三部分組成:
- 中間兩變量關系圖
- 上部x軸變量分布邊際圖
- 右側y軸變量分布邊際圖
-
圖形基本設定
sns.set(style="white",font_scale=1.5)#設定主題,文本大小
g = sns.jointplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,#輸入兩個繪圖變量
color='#098154',#修改顔色
)
g.fig.set_size_inches(10,8)#設定圖尺寸
-
修改中部圖
sns.set(style="white",font_scale=1.5)
g = sns.jointplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
color='#098154',
height=5, #中部圖高度(中部圖為正方形)
ratio=2,#中部圖與邊際圖尺寸比
marker='^',#修改散點marker
)
g.fig.set_size_inches(10,8)
-
修改邊際圖
邊際圖是一個直方圖hist,更多詳細設定可參考::
sns.set(style="white",font_scale=1.5)
g = sns.jointplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
color='#098154',
marginal_kws=dict(bins=15, #hist箱子個數
kde=True,#開啟核密度圖
color='#c72e29',#直方圖hist填充色
),
)
g.fig.set_size_inches(10,8)
-
kind參數設定
包含五個選項,即'scatter,reg,resid,kde,hex',預設為scatter,下面看看各類是什麼樣子。
for i in list('scatter,reg,resid,kde,hex'.split(',')):
sns.set(style="white",font_scale=1.5)
g = sns.jointplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
color='#098154',
kind=i,#scatter(預設),可選reg,resid,kde,hex
)
g.fig.set_size_inches(10,8)
-
給中部散點圖加上核密度圖
sns.set(style="white",font_scale=1.5)#設定主題,文本大小
#sns.set_style("white", {"axes.facecolor": "#d5e4eb"})#自定義背景色
g = sns.jointplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,#輸入兩個繪圖變量
color='#098154',
)
g.fig.set_size_inches(10,8)#設定圖尺寸
g.plot_joint(sns.kdeplot, zorder=0, n_levels=6,color='red')
以上隻是基礎玩法,進階玩法還得seaborn.jointplot的底層seaborn.JointGrid上場
3、seaborn.JointGrid
class seaborn.JointGrid(x, y, data=None, height=6, ratio=5, space=0.2, dropna=True, xlim=None, ylim=None, size=None)
seaborn.JointGrid是seaborn.jointplot的底層,個人了解類似于seaborn的底層是matplotlib一般。
-
sns.JointGrid基礎使用
g = sns.JointGrid(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,#一張畫布
)
g = g.plot(sns.regplot, sns.distplot,)#畫布上繪制三張圖
g.fig.set_size_inches(10,8)#設定圖尺寸
-
分别繪制中心及邊際圖
g = sns.JointGrid(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,)
g.fig.set_size_inches(10,8)
g = g.plot_joint(sns.scatterplot, color='g',marker='$\clubsuit$',s=340)#中心繪制散點圖
g = g.plot_marginals(sns.distplot, kde=True, color='#c72e29')#邊際繪制hist直方圖
-
分别個性化繪制邊際圖
g = sns.JointGrid(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, space=0)
g.fig.set_size_inches(10,8)
g = g.plot_joint(sns.scatterplot, color='g',marker='$\clubsuit$',s=340)
#x軸方向邊際圖
g.ax_marg_x.hist(pd_iris['sepal length(cm)'], color='#01a2d9', alpha=1,
bins=15,
)
#y軸方向邊際圖
g.ax_marg_y.hist(pd_iris['sepal width(cm)'], color='#c72e29', alpha=1,
orientation="horizontal",
bins=15)
plt.show()
-
分别繪制x軸方向及y軸方向邊際圖
#每個邊際圖是一個matplotlib.axes._subplots.AxesSubplot對象,可自由發揮繪制想要的圖
g = sns.JointGrid(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, space=0)
g.fig.set_size_inches(10,8)
g = g.plot_joint(sns.scatterplot, color='g',marker='$\clubsuit$',s=340)
#x軸方向邊際圖
g.ax_marg_x.hist(pd_iris['sepal length(cm)'], color='#01a2d9', alpha=1,bins=15)
#y軸方向繪制箱圖
g.ax_marg_y.boxplot(pd_iris['sepal width(cm)'],boxprops={'color':'r',})
plt.show()
參考資料:
- http://seaborn.pydata.org/generated/seaborn.jointplot.html#seaborn.jointplot
- http://seaborn.pydata.org/generated/seaborn.JointGrid.html#seaborn.JointGrid