【2.1.9】成对图(pairwise-plot)
成对图是探索性分析中的最爱,以理解所有可能的数字变量对之间的关系。 它是双变量分析的必备工具。
例一
# Load Dataset
df = sns.load_dataset('iris')
# Plot
plt.figure(figsize=(10,8), dpi= 80)
sns.pairplot(df, kind="scatter", hue="species", plot_kws=dict(s=80, edgecolor="white", linewidth=2.5))
plt.show()
例2
# Load Dataset
df = sns.load_dataset('iris')
# Plot
plt.figure(figsize=(10,8), dpi= 80)
sns.pairplot(df, kind="reg", hue="species")
plt.show()
例3
infile = 'hsc.csv'
df = pd.read_csv(infile)
import numpy as np
import matplotlib.pyplot as plt
# Seaborn visualization library
import seaborn as sns
# Create the default pairplot
#sns.pairplot(df)
# Function to calculate correlation coefficient between two arrays
def corr(x, y, **kwargs):
# Calculate the value
#remove nan
dt = {'x':x,'y':y}
df_1 = pd.DataFrame(dt)
df_2 = df_1.dropna()
x = df_2['x']
y = df_2['y']
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)
grid = sns.PairGrid(data= df)
# Map the plots to the locations
grid = grid.map_lower(plt.scatter)
grid = grid.map_lower(corr)
grid = grid.map_upper(sns.kdeplot, cmap = 'Blues')
grid = grid.map_diag(plt.hist, bins = 10, edgecolor = 'k');
plt.savefig(result_pic)
参考资料
这里是一个广告位,,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn