python - 在 seaborn 中订购箱线图 x 轴

标签 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]

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

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

这导致了这个情节: boxplot

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

最佳答案

我想出了答案:

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

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

enter image description here

关于python - 在 seaborn 中订购箱线图 x 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40514921/

相关文章:

Python——sympysolve()返回另一个方程而不是值

python - Matplotlib 表 - 为不同的列分配不同的文本对齐方式

python - seaborn 中的白盒图? (覆盖层不透明)

r - 使用箱线图添加线图

python - Pyenv 无法在 Cygwin 上安装 python : ModuleNotFoundError: No module named '_ctypes'

python - 优化python中的非线性函数

python - 将 SQLAlchemy 与多个自引用外键一起使用

python - 用列表切片数组

Python绘制极坐标方程

r - 分组箱线图 r ggplot2