我将如何在像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()
留在 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()
关于python - 在群图之上绘制误差线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44506136/