[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');

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()