在swarmplot

时间:2017-06-12 18:15:56

标签: python pandas matplotlib seaborn

我如何在seaborn docs中绘制像这样的swarmplot上方的平均值和错误标记?

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函数(没有显示所有数据)或使用boxplot等,这对于我想做的事情来说太过花哨了。 / p>

1 个答案:

答案 0 :(得分:2)

您没有提到您想要覆盖的误差线,但您可以在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>

相关问题