【问题标题】:Ordering boxplot x-axis in seaborn在seaborn中订购箱线图x轴
【发布时间】:2023-04-10 14:16:01
【问题描述】:

我的数据框round_data 如下所示:

      error                         username                    task_path
0      0.02  n49vq14uhvy93i5uw33tf7s1ei07vngozrzlsr6q6cnh8w...    39.png
1      0.10  n49vq14uhvy93i5uw33tf7s1ei07vngozrzlsr6q6cnh8w...    45.png
2      0.15  n49vq14uhvy93i5uw33tf7s1ei07vngozrzlsr6q6cnh8w...    44.png
3     0.25  xdoaztndsxoxk3wycpxxkhaiew3lrsou3eafx3em58uqth...    43.png
...     ...                                                ...       ...
1170  -0.11  9qrz4829q27cu3pskups0vir0ftepql7ynpn6in9hxx3ux...    33.png
1171   0.15  9qrz4829q27cu3pskups0vir0ftepql7ynpn6in9hxx3ux...    34.png


[1198 rows x 3 columns]

我想要一个箱线图,显示每个用户的错误,按他们的平均表现排序。我所拥有的是:

ax = sns.boxplot(
    x='username', 
    y='error', 
    data=round_data,
    whis=np.inf,
    color='c',
    ax=ax
)

这导致了这个情节:

如何按平均误差对 x 轴(即用户)进行排序?

【问题讨论】:

    标签: python matplotlib boxplot seaborn


    【解决方案1】:

    我想出了答案:

    grouped = round_data[round_data.batch==i].groupby('username')
    users_sorted_average = (
        pd.DataFrame({col: vals['absolute_error'] for col, vals in grouped})
        .mean()
        .sort_values(ascending=True)
    )
    

    users_sorted_average 传递给 seaborn 绘图函数中的“order”参数将产生所需的行为:

    ax = sns.boxplot(
        x='username', 
        y='error', 
        data=round_data, 
        whis=np.inf,
        ax=ax,
        color=c,
        order=users_sorted_average.index,
    )
    

    【讨论】:

    • 我正试图弄清楚如何应用它。如果我能在我自己的数据上完成这项工作,那就太棒了。我想按中值排序。很遗憾这个功能没有内置到库中。
    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 2021-06-24
    • 2020-12-21
    • 2016-09-03
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多