0

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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
giorgio-p
  • 91
  • 1
  • 5

1 Answers1

1

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

mozway
  • 194,879
  • 13
  • 39
  • 75
  • 1
    +1, but I feel obligated to tell OP that these lines make the visualization **much harder** to understand. "Less is more" – tdy Mar 12 '22 at 18:25