python - 如何设置计数图顺序

标签 python seaborn countplot

我正在尝试确定计数图的顺序。 这是我写的代码:

df['new salery'].sort_values()
sns.set(rc={'figure.figsize':(8,6)})
sns.countplot(x='new salery',data=df,palette='viridis')

我得到的情节是: CountPlot

我想做的是下这个订单 -

Low salary, Medium Salary -, Medium Salary, Medium Salary +, High Salary.

最佳答案

我将使该列成为有序的分类。由此,seaborn 自动遵循以下顺序:

df['new salery'] = pd.Categorical(df['new salery'], 
  categories=['light', 'medium-', 'medium', 'medium+', 'large'], ordered=True)
sns.countplot(x='new salery', data=df, palette='viridis')

或者,如果您只想对绘图执行此操作,也可以在函数中使用 order 关键字:

sns.countplot(x='new salery',data=df,palette='viridis', 
              order=['light', 'medium-', 'medium', 'medium+', 'large'])

Ordered countplot

关于python - 如何设置计数图顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54726576/

相关文章:

python - 如何将 pandas 系列转换为 seaborn 条形图

python - 在时间轴上绘制离散组计数

python - 如何更改seaborn图中的条形宽度?

python - Celery Worker 的预定 (eta) 任务关闭时会发生什么情况?

python - PyQt - 重新实现 QSqlTableModel 数据方法时出现问题

python - 如何将多个参数从 pandas 数据帧传递给函数并将结果返回到数据帧中特定位置的数据帧

python - 如果一组值为空,则删除行

python - 有没有一种简单的方法可以在 matplotlib 中为滚动的垂直线设置动画?