seaborn回歸 pairgrid

[Visualizing linear relationships] (http://seaborn.pydata.org/tutorial/regression.html#regression-tutorial)

Python實踐:seaborn的散點圖矩陣(Pairs Plots)可視化數(shù)據(jù)

image.png
image.png
image.png

使用PairGrid類的真正好處在于我們想要創(chuàng)建自定義函數(shù)來將不同的信息映射到圖上。例如,我可能想要將兩個變量之間的Pearson相關(guān)系數(shù)添加到散點圖中。為此,我會編寫一個函數(shù),它接受兩個數(shù)組、計算統(tǒng)計量,然后在圖上繪制它。下面的代碼顯示了這是如何完成的(歸功于這個Stack Overflow答案):

# Function to calculate correlation coefficient between two arrays
def corr(x, y, **kwargs):
    # Calculate the value
    coef = np.corrcoef(x, y)[0][1]
    # Make the label
    label = r'$\rho$ = ' + str(round(coef, 2))
    # Add the label to the plot
    ax = plt.gca()
    ax.annotate(label, xy = (0.2, 0.95), size = 20, xycoords = ax.transAxes)
# Create a pair grid instance
grid = sns.PairGrid(data= df[df['year'] == 2007],
                    vars = ['life_exp', 'log_pop', 'log_gdp_per_cap'], size = 4)

# Map the plots to the locations
grid = grid.map_upper(plt.scatter, color = 'darkred')
grid = grid.map_upper(corr)
grid = grid.map_lower(sns.kdeplot, cmap = 'Reds')
grid = grid.map_diag(plt.hist, bins = 10, edgecolor =  'k', color = 'darkred');

seaborn直方圖、散點圖與回歸分析圖的繪制

image.png


seaborn pairgrid: using kdeplot with 2 hues

image.png
image.png
image.png
from scipy import stats
import seaborn as sns
import matplotlib

def corrfunc(x, y, **kws):
    r, _ = stats.pearsonr(x, y)
    ax = plt.gca()
    # count how many annotations are already present
    n = len([c for c in ax.get_children() if 
                  isinstance(c, matplotlib.text.Annotation)])
    pos = (.1, .9 - .1*n)
    # or make positions for every label by hand
    pos = (.1, .9) if kws['label'] == 'Yes' else (.1,.8)
 ## 需特別注意有沒有l(wèi)abel
    ax.annotate("{}: r = {:.2f}".format(kws['label'],r),
                xy=pos, xycoords=ax.transAxes)

tips = sns.load_dataset("tips")
g = sns.PairGrid(data = tips, vars = ['tip', 'total_bill'], hue="smoker", size=4)
g.map_upper(plt.scatter, s=10)
g.map_diag(sns.distplot, kde=False)
g.map_lower(sns.kdeplot, cmap="Blues_d")
g.map_lower(corrfunc)
g.add_legend()

官網(wǎng) Building structured multi-plot grids

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容