3

Let's say I have the following seaborn swarmplot:

import seaborn as sns

sns.set_theme(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.swarmplot(x="day", y="total_bill", data=tips)

swarmplot

What is a simple way to display the average of each of these swarms on the plots, perhaps with a different symbol, such as an "X"?

Andrea
  • 607
  • 8
  • 21

2 Answers2

3

You can use pandas' groupby to aggregate the means. And then sns.scatterplot to plot them. For some reason, the scatterplot resets the view limits. You can save xlim and ylim before and reset them afterwards. To have the scatterplot on top of the swarmplot, a zorder can be set (tried with Seaborn 0.11.1):

import seaborn as sns

sns.set_theme(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.swarmplot(x="day", y="total_bill", data=tips)
df_means = tips.groupby("day")["total_bill"].agg("mean").reset_index()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
sns.scatterplot(x="day", y="total_bill", marker='X', color='black', s=100, zorder=3, ax=ax, legend=False, data=df_means)
ax.set_xlim(xlim)
ax.set_ylim(ylim)
plt.show()

swarmplot with means

PS: Another workaround to obtain the desired view limits, is first drawing the means (but with zorder at least 4) and afterwards the swarmplot:

ax = sns.scatterplot(x="day", y="total_bill", marker='X', color='black', s=100, zorder=4, legend=False, data=df_means)
sns.swarmplot(x="day", y="total_bill", data=tips, ax=ax)

An alternative is to draw the swarmplot on top of a boxplot, as in the last example on the swarmplot's manual page.

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Thank you @JohanC. Any idea why using your exact code renders a white "X" for the Sunday swarmplot and black for all of the others? (I am also using Seaborn version 0.11.1) – Andrea Apr 07 '21 at 19:17
1

to get the average of values in python, you can do

def avg(arr): # arr is a list of values to get the average of
   return sum(arr) / len(arr)
retsetera
  • 34
  • 3