Skip to content

Box-Plot with outlier jitter #3148

@buhtz

Description

@buhtz

image

What you see in that picture is a workaround for what I really would like to have. When searching the web you often got the combine-boxplot-with-swarmplot-solution. It would IMHO improve seaborn if this could be done via seaborn without a workaround.

The problems with that example are

  1. The outliers are drawn twice (green and red circles). Only draw the jittered outliers (the green ones).
  2. The none-outliers are also drawn. There is no need for them.

This is an MWE to reproduce that picture.

#!/usr/bin/env python3
import random
import pandas
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme()

random.seed(0)

df = pandas.DataFrame({
    'Vals': random.choices(range(200), k=200)})
df_outliers = pandas.DataFrame({
    'Vals': random.choices(range(400, 700), k=20)})

df = pandas.concat([df, df_outliers], axis=0)

flierprops = {
    'marker': 'o',
    'markeredgecolor': 'red',
    'markerfacecolor': 'none'
}

# Usual boxplot
ax = sns.boxplot(y='Vals', data=df, flierprops=flierprops)

# Add jitter with the swarmplot function
ax = sns.swarmplot(y='Vals', data=df, linewidth=.75, color='none', edgecolor='green')
plt.show()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions