sns.jointplot()雙變量圖,也就是雙變量獨自的分布圖,以及這雙變量的相關性圖


數據分析中常用做圖的方式實現相關性分析,即X軸設置為變量A,Y軸設置為變量B,做散點圖,由於散點圖中點的疊加顯示,往往還需要關注每個變量自身的分布情況,jointplot把描述變量的分布圖和變量相關的散點圖組合在一起,是相關性分析最常用的工具,圖片上還能展示回歸曲線,以及相關系數
import statsmodels.api as sm
import seaborn as sns
sns.set(style="darkgrid")
data = sm.datasets.ccard.load_pandas().data
g = sns.jointplot('AVGEXP', 'AGE', data=data, kind="reg",
                 xlim=(0, 1000), ylim=(0, 50), color="m")

 

 下面我們看一下參數以及常見例子

seaborn.jointplot(*, x=None, y=None, data=None, kind='scatter', color=None, height=6, ratio=5, space=0.2,
dropna=False, xlim=None, ylim=None, marginal_ticks=False, joint_kws=None, marginal_kws=None,
hue=None, palette=None, hue_order=None, hue_norm=None, **kwargs)

kind:{ “scatter” | “kde” | “hist” | “hex” | “reg” | “resid” }

主要了解一下這個kind參數,其余的可以參考官網:http://seaborn.pydata.org/generated/seaborn.jointplot.html?highlight=jointplot#seaborn.jointplot

下面代碼全部都是在seaborn0.11.0版本下實現,可以使用

import seaborn as sns 
sns.__version__  #檢查模塊版本

如果不是,則需要升級版本

pip install -U 模塊名   # 這是 python2+ 版本的用法更新模塊
pip3 install -U 模塊名   # 這是 python3+ 版本的用法更新模塊

在最簡單的調用中,只有x和y默認的是散點圖和直方圖

penguins = sns.load_dataset("penguins")
sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm")

 增加hue變量將為散點圖添加條件顏色,並kdeplot()在邊沿軸上繪制單獨的密度曲線(使用

sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")

通過該kind參數可以使用幾種不同的繪圖方法設置kind="kde"將繪制雙變量和單變量KDE

sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species", kind="kde")

 

 設置kind="reg"以添加線性回歸擬合(使用regplot())和單變量KDE曲線

sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", kind="reg")

 

 對於基於bin的關節分布可視化,還有兩個選項。第一個使用kind="hist"histplot()在所有軸上使用

sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", kind="hist")

 

 或者,設置kind="hex"將使用matplotlib.axes.Axes.hexbin()六角形箱來計算雙變量直方圖

sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", kind="hex")

 

 可以將其他關鍵字參數傳遞給基礎圖

sns.jointplot(
    data=penguins, x="bill_length_mm", y="bill_depth_mm",
    marker="+", s=100, marginal_kws=dict(bins=25, fill=False),
)

 

 使用JointGrid參數來控制圖形的大小和布局

sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", height=5, ratio=2, marginal_ticks=True)

 

 要將更多層添加到繪圖上,請使用返回JointGrid對象上的方法jointplot()

g = sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm")
g.plot_joint(sns.kdeplot, color="r", zorder=0, levels=6) g.plot_marginals(sns.rugplot, color="r", height=-.15, clip_on=False)

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM