繁体   English   中英

如何在 matplotlib 中的一个图上从 Pandas 数据框中添加多个条形图?

[英]How do I add multiple bar graphs from a pandas dataframe on one plot in matplotlib?

我有两个不同的数据框:

df_test1 = pd.DataFrame(
    [['<18', 80841], ['18-24', 334725], ['25-44', 698261], ['45-64', 273087], ['65+', 15035]],
    columns = ['age_group', 'total_arrests']
)

在此处输入图片说明

df_test2 = pd.DataFrame(
    [['<18', 33979], ['18-24', 106857], ['25-44', 219324], ['45-64', 80647], ['65+', 4211]],
    columns = ['age_group','total_arrests']
)

在此处输入图片说明

我使用 matplotlib 创建了以下图:

fig, ax = plt.subplots()

ax.bar(df_test1.age_group, df_test1.total_arrests, color = 'seagreen')
ax.bar(df_test2.age_group, df_test2.total_arrests, color = 'lightgreen')
ax.set_xlabel('Age Group')
ax.set_ylabel('Number of Arrests')
ax.set_title('Arrests vs. Felony Arrests by Age Group')
plt.xticks(rotation=0)
plt.legend(['All Arressts', 'Felony Arrests'])

ax.yaxis.set_major_formatter(
ticker.FuncFormatter(lambda y,p: format(int(y), ','))
)

for i,j in zip(df_test1.age_group, df_test1.total_arrests):
    ax.annotate(format(j, ','), xy=(i,j))

for i,j in zip(df_test2.age_group, df_test2.total_arrests):
    ax.annotate(format(j, ','), xy=(i,j))

plt.show()

在此处输入图片说明

我期待 2 个单独的条形图,每个数据df_test1.total_arrests列一个, df_test1.total_arrestsdf_test2.total_arrests但我得到了一个堆积条形图。 我怎样才能得到一个条形相邻的图表,类似于这里的图表Matplotlib 在一个图表中绘制多个条形 我尝试将我的代码调整为该示例中的代码,但我无法理解。

只有两个酒吧,这相当容易。 解决方案是将刻度线“边缘”上的条形对齐,一个条形向左对齐,另一个向右对齐。

重复相同的逻辑以正确对齐注释。 其中一半左对齐,其他右对齐

fig, ax = plt.subplots()

ax.bar(df_test1.age_group, df_test1.total_arrests, color = 'seagreen', width=0.4, align='edge')
ax.bar(df_test2.age_group, df_test2.total_arrests, color = 'lightgreen', width=-0.4, align='edge')
ax.set_xlabel('Age Group')
ax.set_ylabel('Number of Arrests')
ax.set_title('Arrests vs. Felony Arrests by Age Group')
plt.xticks(rotation=0)
plt.legend(['All Arressts', 'Felony Arrests'])
ax.yaxis.set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda y,p: format(int(y), ','))
)

for i,j in zip(df_test1.age_group, df_test1.total_arrests):
    ax.annotate(format(j, ','), xy=(i,j))

for i,j in zip(df_test2.age_group, df_test2.total_arrests):
    ax.annotate(format(j, ','), xy=(i,j), ha='right')

plt.show()

在此处输入图片说明

如果您有 2 个以上的柱,则情况会更复杂(请参阅上面链接的代码)。 您将更轻松地使用seaborn ,但您必须稍微转换您的数据seaborn

df = pd.merge(left=df_test1, right=df_test2, on='age_group')

df.columns=['age_group','all_arrests', 'felonies']
df = df.melt(id_vars=['age_group'], var_name='Type', value_name='Number')

fig, ax = plt.subplots()
sns.barplot(y='Number',x='age_group',hue='Type', data=df, hue_order=['felonies','all_arrests'])

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM