在seaborn中订购boxplot x轴

ama*_*ouq 6 python matplotlib boxplot seaborn

我的数据框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]
Run Code Online (Sandbox Code Playgroud)

我想有一个箱线图,显示每个用户的错误,按其平均性能排序.我有的是:

    ax = sns.boxplot(x="username", y="error", data=round_data,
                 whis=np.inf, color="c",ax=ax)
Run Code Online (Sandbox Code Playgroud)

这导致了这个情节: 箱形图

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

ama*_*ouq 11

好的,我想出了答案:

    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)   
Run Code Online (Sandbox Code Playgroud)

传递users_sorted_average给seaborn plot函数中的"order"参数会产生所需的行为:

    ax = sns.boxplot(x="username", y="error", data=round_data,
                 whis=np.inf,ax=ax,color=c,order=users_sorted_average.index)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 我正在尝试弄清楚如何应用它。如果我能根据自己的数据完成这项工作,那就太棒了。我想按中值排序。遗憾的是这个功能没有内置到库中。 (2认同)