I got this script to pairplot a dataframe with seaborn. Instead of displaying the pearsonr, I'd like to square it and display the r².
And to display the lineregress equation on each plot.
import numpy as np
from lecture_fichier import lire_tableau
np.set_printoptions(precision=4, suppress=True)
import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt
from scipy.stats import pearsonr
##pearsonr
def r2(x, y, ax=None, **kws):
"""Plot the correlation coefficient in the top left hand corner of a plot."""
r, _ = pearsonr(x, y)
ax = ax or plt.gca()
ax.annotate(f'r² = {r:.2F}', xy=(.1, .9), xycoords=ax.transAxes)
##array datas
E = lire_tableau("data/Mv.txt")
liste1 = np.array(E).ravel().tolist()
F = lire_tableau("data/Es.txt")
liste2 = np.array(F).ravel().tolist()
G = lire_tableau("data/Ed.txt")
liste3 = np.array(G).ravel().tolist()
T = lire_tableau("data/Ef.txt")
liste4 = np.array(T).ravel().tolist()
## dataframe
df = pd.DataFrame(list(zip(liste1, liste2, liste3, liste4)),
columns=['Mv', 'f stat', 't dyn', 'f dyn'])
g = sn.pairplot(df, kind='reg', diag_kind='kde', height=1.5)
g.map_lower(r2)
# remove upper triangle plots
for i, j in zip(*np.triu_indices_from(g.axes, 1)):
g.axes[i, j].set_visible(False)
plt.show()