Created
January 15, 2025 03:17
-
-
Save khanhhd2211/8d2c703b8cfdc0cada140662b7f586fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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