Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save khanhhd2211/8d2c703b8cfdc0cada140662b7f586fd to your computer and use it in GitHub Desktop.
Save khanhhd2211/8d2c703b8cfdc0cada140662b7f586fd to your computer and use it in GitHub Desktop.
from scipy.stats import pearsonr
import matplotlib.pyplot as plt
def corrfunc(x, y, dropna=True, ax=None, **kws):
"""Plot the correlation coefficient in the top left hand corner of a plot."""
if dropna == True:
x = x.dropna()
y = y.dropna()
r, _ = pearsonr(x, y)
ax = ax or plt.gca()
ax.annotate(
f'ρ = {r:.2f}',
xy=(.05, .9),
xycoords=ax.transAxes,
bbox=dict(boxstyle='round,pad=0.2', fc='w', alpha=1)
)
#### Cách dùng
df = sns.load_dataset("penguins")
g = sns.pairplot(
df,
kind="reg",
hue="species",
plot_kws={
"scatter_kws": {
"alpha": 0.2
}
}
)
g.map_lower(corrfunc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment