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

Ale*_*Ash 3 python matplotlib seaborn

我正在seabornPython sns.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')
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

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

gmd*_*mds 5

I believe there is no clean way to do this, because seaborn does not expose keyword arguments for the fill_between call that plots the confidence interval.

However, it can be done by modifying the label attribute of the PolyCollection directly:

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明