pythonmatplotlibseabornboxplotswarmplot

How to overlay data points on seaborn figure-level boxplots


I have the following dataframe and plot below - and I want to add data points for each box plot in the factorplot but I am having trouble combining box and strip plots in to the same graph (i.e. they dont overlay, they appear below each other). Is there a solution for this?

import pandas as pd
import datetime

idx = pd.date_range('01-01-2020', '01-25-2020')

d = pd.Series({'01-01-2020': 1,
               '01-25-2020': 1})

d.index = pd.DatetimeIndex(d.index)

d = d.reindex(idx, fill_value=0)
df = pd.DataFrame(d).rename_axis("dt").reset_index()
df.drop(columns=df.columns[1], inplace=True)

# calculate week number 
df["week"] = df["dt"].dt.week

# create column counts for 'nhsbt centres'
df["A"] = np.random.randint(0, 50, df.shape[0])
df["B"] = np.random.randint(0, 30, df.shape[0])
df["C"] = np.random.randint(0, 20, df.shape[0])

# melt dataframe 
df1 = df[["A", "B", "C", "week"]]
df1 = df1.set_index("week")
df1 = df1.melt(ignore_index=False)
df1["week"] = df1.index

# make boxplot
sns.factorplot("week", "value", col="variable", data=df1, kind="box")

Solution

  • import seaborn as sns
    
    # make boxplot with data from the OP
    g = sns.catplot(x="week", y="value", col="variable", data=df1, kind="box")
    g.map(sns.swarmplot, 'week', 'value', color='k', order=sorted(df1.week.unique()))
    

    enter image description here