我如何使用 seaborn 绘制它?

How do I plot this using seaborn?

import matplotlib.pyplot as plt
import seaborn as sns

rankings_by_age = star_wars.groupby("Age").agg(np.mean).iloc[:,8:]

age_first = rankings_by_age.iloc[0, :].values
age_second = rankings_by_age.iloc[1, :].values
age_third = rankings_by_age.iloc[2, :].values
age_fourth = rankings_by_age.iloc[3, :].values

fig, ax = plt.subplots(figsize=(12, 9))

ind = np.arange(6)
width = 0.2

rects_1 = ax.bar(ind, age_first, width, color=(114/255,158/255,206/255), 
alpha=.8)
rects_2 = ax.bar(ind+width, age_second, width, color=
(255/255,158/255,74/255), alpha=.8)
rects_3 = ax.bar(ind+2*width, age_third, width, color=
(103/255,191/255,92/255), alpha=.8)
rects_4 = ax.bar(ind+3*width, age_fourth, width, color=
(237/255,102/255,93/255), alpha=.8)

ax.set_title("Star Wars Film Rankings by Age")
ax.set_ylabel("Ranking")
ax.set_xticks(ind)
ax.set_xticklabels(titles, rotation=45)

ax.tick_params(top='off', right='off', left='off', bottom='off')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

ax.legend((rects_1[0], rects_2[0], rects_3[0], rects_4[0]), ('18-29', '30-
44', '45-60', '> 60'), title="Age")

plt.show()

我想使用 seaborn 复制此图,但我不确定如何为每个类别绘制多个条形图。我知道如何一次使用一个年龄组来做到这一点,但是每个年龄组获得一个以上的酒吧似乎很棘手。任何帮助,将不胜感激。

引用 seaborn bar plot documentation,您可以使用 hue 参数来确定条形应按数据框的哪一列进行分组。

import seaborn.apionly as sns
import matplotlib.pyplot as plt

df = sns.load_dataset("tips")
ax = sns.barplot(data=df, x="day", y="total_bill", hue="sex")

plt.show()