【问题标题】:Show confidence interval in legend of plot in Python / Seaborn在 Python / Seaborn 的图例中显示置信区间
【发布时间】:2019-04-18 06:59:48
【问题描述】:

我在 Python 上使用 seabornsns.regplot 函数生成一些带有线性回归和置信区间的散点图。我可以找到一种在图例中显示回归线的方法,但我还想在图例中添加置信区间(以透明的蓝色作为参考颜色)。

这是我拥有的代码和目前得到的结果。

Tobin_Nationality_Reg = sns.regplot(x="Nationality_Index_Normalized",
                        y="Tobins_Q_2017",
                        data=Scatter_Plot,
                        line_kws={'label':'Regression line'})

plt.xlabel("Nationality Index")
plt.ylabel("Tobin's Q")
plt.legend()`
plt.savefig('Tobin_Nationality_Reg.png')

这是我目前得到的输出: 散点图

有人知道我该怎么做吗?提前致谢。

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    我相信没有干净的方法可以做到这一点,因为seaborn 不会为绘制置信区间的fill_between 调用公开关键字参数。

    不过,可以直接修改PolyCollectionlabel属性:

    x, y = np.random.rand(2, 20)
    
    ax = sns.regplot(x, y, line_kws={'label': 'Regression line'})
    ax.collections[1].set_label('Confidence interval')
    ax.legend()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-27
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      相关资源
      最近更新 更多