简体   繁体   English

在seaborn中订购箱线图x轴

[英]Ordering boxplot x-axis in seaborn

My dataframe round_data looks like this:我的数据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]

I want to have a boxplot showing the error of each user sorted by their average performance.我想要一个箱线图,显示每个用户的错误,按他们的平均表现排序。 What I have is:我所拥有的是:

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

which results into this plot:这导致这个情节: 箱线图

How can I sort the x-axis (ie, users) by mean error?如何按平均误差对 x 轴(即用户)进行排序?

I figured out the answer:我想出了答案:

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

Passing users_sorted_average for the "order" parameter in the seaborn plot function would give the desired behavior:为 seaborn 绘图函数中的“order”参数传递users_sorted_average将提供所需的行为:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM