python - 在群图之上绘制误差线

标签 python matplotlib seaborn errorbar swarmplot

我将如何在像seaborn文档中的这样的群图之上绘制平均值和误差条?

import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset("tips")
sns.swarmplot(x="day", y="total_bill", data=tips);
plt.show()

如果不使用 errobar 函数(不显示所有数据)或使用箱线图等,我无法找到一种绘制误差线的简单方法,这对于我想做的事情来说太花哨了。

最佳答案

您没有提及您希望误差线覆盖什么,但您可以在 swarmplot 之上绘制样本均值±标准差,并仅使用 plt.errorbar 通过

mean = tips.groupby('day').total_bill.mean()
std = tips.groupby('day').total_bill.std() / np.sqrt(tips.groupby('day').total_bill.count())
sns.swarmplot(x='day', y='total_bill', data=tips, zorder=1)
plt.errorbar(range(len(mean)), mean, yerr=std)
plt.show()

<code>sns.swarmplot</code> and <code>plt.errorbar</code>

留在 seaborn 的世界中的另一个选择是 sns.pointplot它通过引导自动生成置信区间:

sns.swarmplot(x='day', y='total_bill', data=tips, zorder=1)
sns.pointplot(x='day', y='total_bill', data=tips, ci=68)
plt.show()

<code>sns.swarmplot</code> and <code>sns.pointplot</code>

关于python - 在群图之上绘制误差线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44506136/

相关文章:

python - 如何在散点图中按所需顺序对 y 轴进行排序?

python - plot Artists如何重用(Line2D)?

matplotlib - matplotlib 中颜色条的几何形状

Python 使用 matplotlib 绘制大矩阵

python - 海伯恩 : How to get the count in y axis for distplot using PairGrid

python - 如何在python中绘制时间序列数组,并显示置信区间?

python - 如何绘制多个 Pandas 列

python - 在python中对mysql数据库获取的日期执行操作

python - 弃用警告 : executable_path has been deprecated selenium python

python - 在 run -it/bin/bash 之外的 docker 容器中运行 python 脚本的问题