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

标签 python matplotlib seaborn

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

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

enter image description here

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

最佳答案

我认为没有干净的方法可以做到这一点,因为 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()

enter image description here

关于python - 在 Python/Seaborn 的图例中显示置信区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55740664/

相关文章:

Python:群图覆盖箱形图中的中线

python - TypeError:在绘制 seaborn.regplot 时,无法根据规则 ('int64' 将数组数据从 dtype ('int32' )转换为 dtype 'safe' )

python - 绘制时间序列数据,其中每小时变化是不同列中的值

python - matplotlib 的二维插值频谱图差异的来源是什么?

python - 使用缺口形状时 matplotlibs boxplot 的奇怪行为

python - FuncAnimation 仅在窗口大小调整后启动

python - 条形图绘制总和,而不是列的平均值

python - Django 在产品服务器中打印

python - django-tables2 用查询淹没数据库

python - 为什么我没有看到 python 中调用析构函数