Search code examples
pythonpandasmatplotlibbar-chartgrouped-bar-chart

How to make a grouped bar chart from a Pandas DataFrame


How can I use pyplot to plot this dataframe:

          Team   Boys  Girls
0        Sharks    5       5
1         Lions    3       7

data = {'Team': ['Sharks', 'Lions'], 'Boys': [5, 5], 'Girls': [5, 6] }
df = pd.DataFrame(data)

so that I can end up with something like this where there are two bars per team that denote the boys/girls. enter image description here

It seems to be a messy process with pyplot, so if there is an easier way I am all ears.


Solution

  • You can use pandas.DataFrame.plot.bar with rot=0 to rotate the xticks

    df.set_index('Team').plot.bar(rot = 0)
    plt.show()
    

    plot