在Seaborn订购Boxplot X轴 [英] Ordering boxplot x-axis in seaborn

查看:76
本文介绍了在Seaborn订购Boxplot X轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框 round_data 看起来像这样:

My dataframe round_data looks like this:

      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:

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

How can I sort the x-axis (i.e., users) by mean error?

推荐答案

好的,我想出了答案:

    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 plot函数中的 order参数将给出所需的行为:

passing users_sorted_average for the "order" parameter in the seaborn plot function would give the desired behavior:

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

< a href = https://i.stack.imgur.com/r3INe.png rel = noreferrer>

这篇关于在Seaborn订购Boxplot X轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆