简体   繁体   中英

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:

city zone category searches_ok searches_null
Buenos Aires Zone1 Books 12 6
Buenos Aires Zone1 Home 7 0
Buenos Aires Zone2 Books 5 7
Rosario Zone7 Home 25 1
Rio de Janeiro Zone8 Tech 55 3
Rio de Janeiro Zone9 Books 12 7
Sao Paulo Zone15 Tech 34 8

How would go about plotting this in a grouped bar chart? I would like for there to be 2 bars (searches_ok and searches_null) for each "combination" of city/zone/category.

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?

Try making your x columns the index:

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

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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