【问题标题】:Python Seaborn jointplot does not show the correlation coefficient and p-value on the chartPython Seaborn 联合图未在图表上显示相关系数和 p 值
【发布时间】:2019-02-06 15:37:37
【问题描述】:

我正在尝试用下面的样本绘制联合图,我看到它应该在图表上显示相关系数和 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()

【问题讨论】:

  • 请提供可以重现一些图的代码以帮助您。也向我们展示你到底想要什么作为最终的数字。目前您只是在绘制数据。您看到的样本可能使用其他方式显示了值。其中之一是plt.textthis 页面上的所有图均未显示任何值
  • 另外,将系数和 p 值放在图表上的示例在哪里?
  • seaborn 移除了绘制 p 值的功能。不过,您仍然可以使用已弃用的 stat_func 参数。但是,一般来说,建议您计算要在绘图上显示的任何值并通过ax.text 添加它。
  • 谢谢大家。我不太清楚如何将 seaborn 与 ax.text 一起使用,所以我最终使用 Jointplot 的 annotate 方法来显示这一点。

标签: python matplotlib seaborn


【解决方案1】:

我最终使用下面来绘制

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()

【讨论】:

  • 被低估的问题/答案对。令人讨厌的是,他们毫不客气地弃用了 stat_func 并且在文档中没有明显的解决方法。我感觉这个 SE 帖子随着时间的推移越来越受欢迎……
  • 为我工作,但收到此警告:JointGrid annotation is deprecated and will be removed in a future release.
  • UserWarning:JointGrid 注释已弃用,将在未来版本中删除。警告.warn(UserWarning(msg))
  • 这是documented 方法(这也许可以解释为什么很少有人对这对 Q/A 给予更高的评价)
【解决方案2】:

在 seaborn 的 >=0.11 版本中,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)])

【讨论】:

  • 到目前为止,这是唯一真正提供解决方法的答案。
【解决方案3】:

Seaborn v0.9.0(2018 年 7 月)已弃用此功能:

已弃用 JointGrid 的统计注释组件。该方法仍然可用,但将在未来版本中删除。 (source)

【讨论】:

  • 这究竟是什么原因?
【解决方案4】:

您现在可以忽略警告。 另外,我们可以直接在绘图上调用annotate方法,而不需要先创建对象。

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()

【讨论】:

    【解决方案5】:

    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)
    

    【讨论】:

      【解决方案6】:

      如果你想在 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)
      

      【讨论】:

      • 当我尝试这个解决方案时,我收到 NameError: name 'ax' is not defined。
      • 嗯?不知道你是什么人。我叫 j.ax_joint 而不是 j.ax 所以我不明白为什么你会得到 'ax' 没有定义。您使用的是最新版本的 seaborn 吗?我是,或者至少是较新的版本之一,所以也许你可以尝试更新你的 seaborn 版本?我意识到有些代码只适用于更新的 seaborn 版本
      • 我使用 Jupyter Notebook,所以应该更新它们。
      猜你喜欢
      • 2019-01-07
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 2016-09-29
      相关资源
      最近更新 更多