在 matplotlib 的分组栏中排序类别

Order categories in a grouped bar in matplotlib

我正在尝试绘制一个 groupby-pandas-dataframe,其中我有一个分类变量,我想通过它来订购条形图。

我正在做的示例代码:

import pandas as pd

df = {"month":["Jan", "Jan", "Jan","Feb", "Feb", "Mar"], 
      "cat":["High", "High", "Low", "Medium", "Low", "High"]}

df = pd.DataFrame(df)

df.groupby("month")["cat"].value_counts().unstack(0).plot.bar()

哪些地块:

但是,我想在每个类别中绘制顺序为一月、二月、三月。

任何关于如何实现这一点的帮助将不胜感激。

亲切的问候。

您可以将月份列分类以修复订单:

import pandas as pd

df = {"month": ["Jan", "Jan", "Jan", "Feb", "Feb", "Mar"],
      "cat": ["High", "High", "Low", "Medium", "Low", "High"]}
df = pd.DataFrame(df)
df["month"] = pd.Categorical(df["month"], ["Jan", "Feb", "Mar"])

df.groupby("month")["cat"].value_counts().unstack(0).plot.bar(rot=0)

另一种方法是 select 调用 unstack(0) 后的列顺序:

df.groupby("month")["cat"].value_counts().unstack(0)[["Jan", "Feb", "Mar"]].plot.bar(rot=0)

我建议您使用 seaborn package 从数据框中绘制数据。绘图时组织和排序每个元素非常简单。

首先让我们添加一个列,其中包含每个现有 month/cat 组合的计数:

import pandas as pd

data = {"month":["Jan", "Jan", "Jan","Feb", "Feb", "Mar"], 
      "cat":["High", "High", "Low", "Medium", "Low", "High"]}

df = pd.DataFrame(data)

df = df.value_counts().reset_index().rename(columns={0: 'count'})
print(df)

# output:
# 
#   month     cat  count
# 0   Jan    High      2
# 1   Mar    High      1
# 2   Jan     Low      1
# 3   Feb  Medium      1
# 4   Feb     Low      1

seaborn 绘图就变得很简单:

import matplotlib.pyplot as plt
import seaborn as sns

sns.barplot(
    data=df,
    x='cat',
    y='count', 
    hue='month', 
    order=['Low', 'Medium', 'High'],  # Order of elements in the X-axis 
    hue_order=['Jan', 'Feb', 'Mar'],  # Order of colored bars at each X position
)
plt.show()

输出图像: