Skip to content
Advertisement

How to do a boxplot with individual data points using seaborn

I have a box plot that I create using the following command:

sns.boxplot(y='points_per_block', x='block', data=data, hue='habit_trial')

So the different colors represent whether the trial was a habit trial or not (0,1). I want to also plot the individual data points, which I tried to achieve using:

sns.stripplot(y='points_per_block', x='block', data=data, hue='habit_trial')

The result was the following

enter image description here

I want the individual points to display over the corresponding box plots. Is there a way to do this without resorting to hacking their positions in some manner? The problem comes from the fact that the separation of data using hue works differently for stripplot and boxplot but I would have thought that these would be easily combinable.

Thanks in advance.

Advertisement

Answer

Seaborn functions working with categorical data usually have a dodge= parameter indicating whether data with different hue should be separated a bit. For a boxplot, dodge defaults to True, as it usually would look bad without dodging. For a stripplot defaults to dodge=False.

The following example also shows how the legend can be updated (matplotlib 3.4 is needed for HandlerTuple):

import seaborn as sns
from matplotlib.legend_handler import HandlerTuple

tips = sns.load_dataset("tips")
ax = sns.boxplot(data=tips, x="day", y="total_bill",
                 hue="smoker", hue_order=['Yes', 'No'], boxprops={'alpha': 0.4})
sns.stripplot(data=tips, x="day", y="total_bill",
              hue="smoker", hue_order=['Yes', 'No'], dodge=True, ax=ax)
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles=[(handles[0], handles[2]), (handles[1], handles[3])],
          labels=['Smoker', 'Non-smoker'],
          loc='upper left', handlelength=4,
          handler_map={tuple: HandlerTuple(ndivide=None)})

boxplot and stripplot combined

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement