带有 Python Seaborn 连接点的 Swarmplot

Swarmplot with connected dots with Python Seaborn

我尝试使用结合了群图的箱线图和连接数据点的线来跟踪数据框中的变化。

到目前为止,我只能使用以下代码将箱线图与群图结合起来:

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")

结果是这样的:

我想做的是跟踪每个 ID 连接 A、B、C 和 D 的单个点的变化。

这样我就可以在 4 个条件下跟踪例如 ID 1 的性能。像这样:

不确定我是否理解这个问题,因为箱线图用于聚合信息而不是显示所有值,但如果您想显示所有点:

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

或:

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

要有黑线,使用:

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