SNS catplot(Box plot)根据平均值选择仅显示5个数据点

问题描述 投票:2回答:1

我有一个sns catplot(boxplot)(点击下面的链接)。对于从x轴看到的每个时间窗口,存在多个箱图,每个箱图对应于1个ID。我如何编码使得对于每个时间窗口,在特定时间只显示5个最高均值的ID用于所有时间窗口?谢谢!`

sns.catplot('time_window', hue='ID', y='Time (ms)', data=mo_finaldf, kind="box", showfliers=False)

python-3.x seaborn boxplot
1个回答
1
投票

我找到了问题的答案。基本上,seaborn和Matplotlib没有任何设置,你必须自己拆分数据帧。我所做的是一个groupby后跟一个SQL连接。希望它可以帮助将来遇到同样问题的任何人。

df_to_join = mo_finaldf.groupby(['time_window', 'ID']).agg({"time": {'Mean': 'mean', 'var': 'var'}})\
    ['time'].sort_values(by='Mean', ascending=False).sort_index(level='time_window', sort_remaining=False)
highest_5_mean = df_to_join.groupby(['time_window']).head(5).copy()
highest_5_mean.reset_index(inplace=True)
highest_5_mean.rename(columns={'time': 'Mean'}, inplace=True)

dataset_filtered = pd.merge(mo_finaldf, highest_5_mean, how='inner', left_on=['time_window', 'tap'],
                            right_on=['time_window', 'ID'])
sns.catplot(x='time_window', hue='tap', y='time', data=dataset_filtered, kind="box",
                 showfliers=False)
© www.soinside.com 2019 - 2024. All rights reserved.