python - python pandas 中按组分组的子图

标签 python pandas

我想为以下数据制作子图。我平均并分组在一起。 我想按国家x轴资源y轴平均值进行子统计。

country       resource     average
india         water         76
india         soil          45
india         tree          60
US            water         45
US            soil          70
US            tree          85
Germany       water         76
Germany       soil          65
Germany       water         56

Grouped = df.groupby(['country','resource'])['TTR in minutes'].agg({'average': 'mean'}).reset_index()

我尝试过,但无法在子图中绘制

最佳答案

g = df.groupby('country')

fig, axes = plt.subplots(g.ngroups, sharex=True, figsize=(8, 6))

for i, (country, d) in enumerate(g):
    ax = d.plot.bar(x='resource', y='average', ax=axes[i], title=country)
    ax.legend().remove()

fig.tight_layout()

enter image description here

关于python - python pandas 中按组分组的子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49306221/

相关文章:

python - 尝试读取 docx 文件时出现 UnicodeDecodeError

python - 使用 statsmodel 在 Python 中从 GLM 中提取系数

python - 如何在 python 中平滑地转换颜色?

python - 使用霍夫变换检测圆

python - 读取 "variable=value"(Windows INI) 格式的文件?

python - python中有保留函数吗?我想将 SAS 代码传输到 python

python - 编辑两个 Pandas 列之间的距离

python - 如何在 matplotlib 中为带有子图的图形添加总体标题?

python - 为每个 plt.step 行指定不同的颜色

python - 如何正确地将每一大块数据保存为 Pandas 数据框并将它们相互连接