简体   繁体   English

如何从 dataframe 到 plot 在 x 中包含多个列的分组条形图?

[英]How to plot a grouped bar chart from a dataframe with several columns in x?

This is my first time trying to plot some data in Python. I have a dataframe like this one:这是我第一次尝试 plot Python 中的一些数据。我有一个像这样的 dataframe:

city城市 zone category类别 searches_ok搜索_ok searches_null searches_null
Buenos Aires布宜诺斯艾利斯 Zone1 1区 Books图书 12 12 6 6个
Buenos Aires布宜诺斯艾利斯 Zone1 1区 Home 7 7 0 0
Buenos Aires布宜诺斯艾利斯 Zone2 2区 Books图书 5 5个 7 7
Rosario罗萨里奥 Zone7 7区 Home 25 25 1 1个
Rio de Janeiro里约热内卢 Zone8 8区 Tech科技 55 55 3 3个
Rio de Janeiro里约热内卢 Zone9 9区 Books图书 12 12 7 7
Sao Paulo圣保罗 Zone15 15区 Tech科技 34 34 8 8个

How would go about plotting this in a grouped bar chart? go 如何将其绘制在分组条形图中? I would like for there to be 2 bars (searches_ok and searches_null) for each "combination" of city/zone/category.我希望城市/区域/类别的每个“组合”都有 2 个条形图(searches_ok 和 searches_null)。

Using this:使用这个:

df_arg.set_index("zone")
df.plot(x="zone", y=["searches_ok","searches_null"]

I was able to plot a graph with both columns, but only for the "Zone" column... How would you go about doing that for each combination of city/zone/category?我能够 plot 绘制包含两列的图表,但仅限于“区域”列...您 go 如何针对城市/区域/类别的每个组合执行此操作?

Try making your x columns the index:尝试使您的 x 列成为索引:

df.set_index(['city','zone','category']).plot.bar(rot=45)

Output: Output:

在此处输入图像描述

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

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