pythonpandasmatplotlibbar-chartgrouped-bar-chart

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 ?


Solution

  • Try making your x columns the index:

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

    Output:

    enter image description here