ordering in boxplot according to an object

2020-05-02 18:17发布

I would like to have some grouped boxplot in a pandas df.

var2 is an object, and I would like to display the boxplot in alphabetical order according to var2 order

import seaborn as sns
sns.set_style("whitegrid")   
ax = sns.boxplot(x="var1", y="var2",order=???, data=df)

without putting manually: order=["a","b","c","d","e"]

1条回答
疯言疯语
2楼-- · 2020-05-02 18:22

Not 100% sure what needs to be sorted, but essentially you need to use unique():

order = sorted(df.var1.unique())
ax = sns.boxplot(x="var1", y="var2", order=order, data=df)
查看更多
登录 后发表回答