user2784820
user2784820

Reputation: 875

Python Seaborn jointplot does not show the correlation coefficient and p-value on the chart

I'm trying to plot jointplot with below and from samples I saw it should show the correlation coefficient and p-value on the chart. However it does not show those values on mine. Any advice? thanks.

import seaborn as sns
sns.set(style="darkgrid", color_codes=True)
sns.jointplot('Num of A', ' Ratio B', data = data_df, kind='reg', height=8)
plt.show()

Upvotes: 35

Views: 38153

Answers (6)

sambit9238
sambit9238

Reputation: 321

With version >=0.11 of seaborn, jointgrid annotation is removed so you won't see the pearsonr value.

If needed to display, one way is to calculate the pearsonr and put it in the jointplot as a legend.

for example:

import scipy.stats as stats
graph = sns.jointplot(data=df,x='x', y='y')
r, p = stats.pearsonr(x, y)
# if you choose to write your own legend, then you should adjust the properties then
phantom, = graph.ax_joint.plot([], [], linestyle="", alpha=0)
# here graph is not a ax but a joint grid, so we access the axis through ax_joint method

graph.ax_joint.legend([phantom],['r={:f}, p={:f}'.format(r,p)])

enter image description here

Upvotes: 24

Jonathan Ng
Jonathan Ng

Reputation: 21

If you want to use annotation in seaborn, one way would be to do something like:

import seaborn as sns
from matplotlib import pyplot as plt
from scipy import stats

sns.set(style="darkgrid", color_codes=True)
j = sns.jointplot('Num of A', ' Ratio B', data = data_df, kind='reg', height=8)
r, p = stats.pearsonr(data_df['Num of A'], data_used['Ratio B'])
# j.ax_joint allows me to access the matplotlib ax object, along with its
# associated methods and attributes
j.ax_joint.annotate('r = {:.2f} '.format(r), xy=(.1, .1), xycoords=ax.transAxes)
j.ax_joint.annotate('p = {:.2e}'.format(p), xy=(.4, .1), xycoords=ax.transAxes)

Upvotes: 0

Adegite Taiwo
Adegite Taiwo

Reputation: 39

Seaborn gives a parameter that helps with that

import seaborn as sns
import scipy.stats as stat
from warnings import filterwarnings

#this will help ignore warnings
filterwarnings ('ignore')

#jointplot

sns.jointplot('x', 'y', data=data, 
stat_func=stat.pearsonr)

Upvotes: 1

Kyle A.
Kyle A.

Reputation: 113

This functionality was deprecated in Seaborn v0.9.0 (July 2018):

Deprecated the statistical annotation component of JointGrid. The method is still available but will be removed in a future version. (source)

Upvotes: 6

Tushar Sharma
Tushar Sharma

Reputation: 303

You can ignore warnings for now. Also, we can directly call the annotate method on the plot without creating the object first.

import seaborn as sns
import scipy.stats as stats
from warnings import filterwarnings
filterwarnings('ignore')

sns.set(style="darkgrid", color_codes=True)
sns.jointplot('Num of A', ' Ratio B', data = data_df, kind='reg', height=8).annotate(stats.pearsonr)
plt.show()

Upvotes: 3

user2784820
user2784820

Reputation: 875

I ended up using below to plot

import seaborn as sns
import scipy.stats as stats

sns.set(style="darkgrid", color_codes=True)
j = sns.jointplot('Num of A', ' Ratio B', data = data_df, kind='reg', height=8)
j.annotate(stats.pearsonr)
plt.show()

Upvotes: 38

Related Questions