Home > other >  Getting separate means line in a single plot
Getting separate means line in a single plot

Time:12-31

I'm working on this figure and wondering if it's possible to get a mean line (like the black line in the figure) but for each concentration group.

Sorry I can't share the data with you. Ideas?

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

p=sns.swarmplot(x="Class", y="DS", data=dfx, size=9, palette="summer_r", hue="Conc")
sns.boxplot(showmeans=True,
            meanline=True,
            meanprops={'color': 'k', 'ls': '-', 'lw': 2},
            medianprops={'visible': False},
            whiskerprops={'visible': False},
            zorder=10,
            x="Class",
            y="DS",
            data=dfx,
            showfliers=False,
            showbox=False,
            showcaps=False,
            ax=p)
plt.show()

How it is right now: enter image description here

How I want it to be (red arrows are just to catch your attention): enter image description here

CodePudding user response:

You could loop through the different conc values, and call the dummy boxplot for each of them.

Here is an example using the standard tips dataset:

import seaborn as sns

tips = sns.load_dataset('tips')
ax = sns.swarmplot(x="day", y="tip", data=tips, size=3, palette="summer_r", hue="sex")
categories = tips["sex"].unique()
colors = sns.color_palette('summer_r', len(categories))
for cat, color in zip(categories, colors):
    sns.boxplot(showmeans=True,
                meanline=True,
                meanprops={'color': color, 'ls': '-', 'lw': 2},
                medianprops={'visible': False},
                whiskerprops={'visible': False},
                zorder=3,
                x="day",
                y="tip",
                data=tips[tips["sex"] == cat],
                showfliers=False,
                showbox=False,
                showcaps=False,
                ax=ax)

sns.swarmplot mean line per hue value

  • Related