[英]Python Seaborn jointplot does not show the correlation coefficient and p-value on the chart
我正在尝试用下面的样本绘制联合图,我看到它应该在图表上显示相关系数和 p 值。 但是它没有在我的身上显示这些值。 有什么建议吗? 谢谢。
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()
我最终使用下面的绘图
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()
随着版本 >=0.11 的 seaborn,jointgrid 注释被删除,因此您将看不到 pearsonr 值。
如果需要显示,一种方法是计算 pearsonr 并将其作为图例放入 jointplot 中。
例如:
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)])
此功能已在 Seaborn v0.9.0(2018 年 7 月)中弃用:
弃用了 JointGrid 的统计标注组件。 该方法仍然可用,但将在以后的版本中删除。 (来源)
您现在可以忽略警告。 此外,我们可以直接在绘图上调用注释方法,而无需先创建对象。
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()
Seaborn 提供了一个有助于此的参数
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)
如果你想在 seaborn 中使用注释,一种方法是做类似的事情:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.