Search code examples
pythonseabornboxplotswarmplot

how to arrange dots from seaborn swarmplot and respective boxplot, after grouping


Does anyone know of a way to place the dots of grouped swarmplots, on their respective boxplot? As it is right now, the grouping causes the dots to appear on the midline of the two groups (see attached image).

Code:

sns.set(rc={'figure.figsize':(15.7,8.27)})
ax = sns.boxplot(data=mice_20191203, x="group", y="engraft", showfliers = False, hue="status")
ax = sns.swarmplot(data=mice_20191203, x="group", y="engraft", color=".25", size=6)

ax.set_title(label='K562 cells in NSG mice', fontsize=20)
ax.set_xlabel(xlabel='injected cells', fontsize=16)
ax.set_ylabel(ylabel='human CD45 (%)', fontsize=16)
plt.xticks(rotation=30)

plt.show()

Output: grouped boxplot and swarmplot

Thanks a bunch in advance!


Solution

  • In addition to using hue=, you have to use dodge=True in the call to swarmplot. See the documentation for swarmplot.