在Python / Seaborn中显示情节图例中的置信区间

问题描述 投票:3回答:1

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

这是我的代码和到目前为止的结果。

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

这是我目前获得的输出:Scatter Plot

enter image description here

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

python matplotlib seaborn
1个回答
5
投票

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

但是,可以通过直接修改labelPolyCollection属性来完成:

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.