Last active
November 6, 2021 14:57
-
-
Save CnrLwlss/22f4abbb11e4ab01ae8bb35e041941bb to your computer and use it in GitHub Desktop.
Script for plotting stripplot or swarmplot with barplot overlay, including custom error bars, with matplotlib/Seaborn.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import seaborn as sns | |
tips = sns.load_dataset("tips") | |
print(sns.__version__) | |
print(tips.head()) | |
ax=sns.swarmplot(x="day", y="total_bill", hue="sex", data=tips,split=True) | |
sns.barplot(x="day", y="total_bill", hue="sex", data=tips,capsize=0.1,errwidth=1.25,alpha=0.25,ci=None) | |
xcentres=[0,1,2,3] | |
delt=0.2 | |
xneg=[x-delt for x in xcentres] | |
xpos=[x+delt for x in xcentres] | |
xvals=xneg+xpos | |
xvals.sort() | |
yvals=tips.groupby(["day","sex"]).mean().total_bill | |
yerr=tips.groupby(["day","sex"]).std().total_bill | |
(_, caps, _)=ax.errorbar(x=xvals,y=yvals,yerr=yerr,fmt=None,capsize=4,errwidth=1.25,ecolor="red") | |
for cap in caps: | |
cap.set_markeredgewidth(2) | |
handles, labels = ax.get_legend_handles_labels() | |
l = sns.plt.legend(handles[0:2], labels[0:2]) | |
sns.plt.ylim([0,60]) | |
sns.plt.ylabel("Total Bill ($)") | |
sns.plt.xlabel("") | |
sns.plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While there are still some deprecation warnings showing, I got this to work in 2018 in Python 3.6 Jupyter notebook by removing last line, and putting
%matplotlib inline
at the start of the cell and changing ones above the removed line to below: