簡體   English   中英

在seaborn中訂購箱線圖x軸

[英]Ordering boxplot x-axis in 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
)

這導致這個情節: 箱線圖

如何按平均誤差對 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,
)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM