Swarmplot with connected dots

936 Views Asked by At

I'm try to keep track of changes within a dataframe using a boxplot combined with a swarmplot, and lines connecting data points.

So far I only managed to combine the boxplot with the swarmplot, using this code:

import seaborn as sns
from itertools import cycle

import numpy as np
import pandas as pd

#create random dataframe with 4 conditions
df = pd.DataFrame(data = np.random.random(size=(4,4)), columns = ['A','B','C','D'])
datapoints=pd.melt(df)

#add IDs
ID = cycle(["1","2","3", "4"])
datapoints['ID'] = [next(ID) for IDs in range(len(datapoints))]

#plot values 
sns.boxplot(x="variable", y="value", data=datapoints)
sns.swarmplot(x="variable", y="value", data=datapoints, color=".25")

And the result is this: enter image description here

Waht I would like to do is to keep track of changes connecting the single dots across A, B, C and D per ID.

This way I could follow the performance of for example ID 1 across the 4 conditions. Something like this:

enter image description here

1

There are 1 best solutions below

1
On BEST ANSWER

Not sure I understood the question as boxplot are used to aggregate information rather than showing all values, but if you want to show all points:

sns.lineplot(x="variable", y="value", hue='ID', data=datapoints,
             estimator=None, legend=False)

or:

sns.lineplot(data=df.T, legend=False)

enter image description here

To have black lines, use:

pal = sns.color_palette(['black'], df.shape[1])
sns.lineplot(x="variable", y="value", hue='ID', data=datapoints,
             estimator=None, legend=False, palette=pal)

enter image description here