python - Seaborn Correlation Coefficient on PairGrid -
is there matplotlib or seaborn plot use g.map_lower or g.map_upper correlation coefficient displayed each bivariate plot shown below? plt.text manually mapped below example tedious process.
you can pass function map_*
methods long follows few rules: 1) should plot onto "current" axes, 2) should take 2 vectors positional arguments, , 3) should accept color
keyword argument (optionally using it, if want compatible hue
option).
so in case need define little corrfunc
function , map across axes want have annotated:
import numpy np scipy import stats import pandas pd import seaborn sns import matplotlib.pyplot plt sns.set(style="white") mean = np.zeros(3) cov = np.random.uniform(.2, .4, (3, 3)) cov += cov.t cov[np.diag_indices(3)] = 1 data = np.random.multivariate_normal(mean, cov, 100) df = pd.dataframe(data, columns=["x", "y", "z"]) def corrfunc(x, y, **kws): r, _ = stats.pearsonr(x, y) ax = plt.gca() ax.annotate("r = {:.2f}".format(r), xy=(.1, .9), xycoords=ax.transaxes) g = sns.pairgrid(df, palette=["red"]) 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)
Comments
Post a Comment