I have the below data:
prop_tenure prop_12m prop_6m
0.00 0.00 0.00
0.00 0.00 0.00
0.06 0.06 0.10
0.38 0.38 0.25
0.61 0.61 0.66
0.01 0.01 0.02
0.10 0.10 0.12
0.04 0.04 0.04
0.22 0.22 0.22
and I am doing a pairplot as below:
sns.pairplot(data)
plt.show()
However I would like to display the correlation coefficient among the variables and if possible the skewness and kurtosis of each variable. How do you do that in seaborn?
Correlation. The seaborn library allows to draw a correlation matrix through the pairplot() function. The parameters to create the example graphs are: data : dataframe.
pairplot() : To plot multiple pairwise bivariate distributions in a dataset, you can use the pairplot() function. This shows the relationship for (n, 2) combination of variable in a DataFrame as a matrix of plots and the diagonal plots are the univariate plots.
You can plot correlation between two columns of pandas dataframe using sns. regplot(x=df['column_1'], y=df['column_2']) snippet. You can see the correlation of the two columns of the dataframe as a scatterplot.
As far as I'm aware, there is no out of the box function to do this, you'll have to create your own:
from scipy.stats import pearsonr
import matplotlib.pyplot as plt
def corrfunc(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:.2f}', xy=(.1, .9), xycoords=ax.transAxes)
Example using your input:
import seaborn as sns; sns.set(style='white')
import pandas as pd
data = {'prop_tenure': [0.0, 0.0, 0.06, 0.38, 0.61, 0.01, 0.10, 0.04, 0.22],
'prop_12m': [0.0, 0.0, 0.06, 0.38, 0.61, 0.01, 0.10, 0.04, 0.22],
'prop_6m': [0.0, 0.0, 0.10, 0.25, 0.66, 0.02, 0.12, 0.04, 0.22]}
df = pd.DataFrame(data)
g = sns.pairplot(df)
g.map_lower(corrfunc)
plt.show()
Just to mention, for seaborn in more recent version (>0.11.0
) the answer above doesn't work anymore. But you need to add a hue=None
to make it work again.
def corrfunc(x, y, hue=None, 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:.2f}', xy=(.1, .9), xycoords=ax.transAxes)
Reference this issue https://github.com/mwaskom/seaborn/issues/2307#issuecomment-702980853
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With