Search code examples
pythonpandasmatplotlibbar-chartstacked-bar-chart

How to create a proportional horizontal stacked bar chart with percentages


I have been struggling to create a horizontal bar chart using python, pandas, and matplotlib.

Ideally I'd like to have percentages on the chart as well.

I merged 2 datasets together to get this:

# dataframe
df = pd.DataFrame({'Moving Violation': [103281, 75376, 66957, 73071, 244090],
                   'Other Violations': [54165, 75619, 48567, 33587, 127639]},
                  index=['asian/pacific islander', 'black', 'hispanic', 'other', 'white'])

                        Moving Violation  Other Violations
asian/pacific islander            103281             54165
black                              75376             75619
hispanic                           66957             48567
other                              73071             33587
white                             244090            127639

I am now looking to create a stacked bar chart that looks something like this:

Bar chart I want it to look like

I am struggling to figure this out. I have used plt.barh(), but it has deemed to not work very well.


Solution

  • You can do it through temporary df with percentage proportions of your data as in the example below. Dataframe I created has different data, but is the same type as yours.

    # df similar to what you have
    df = pd.DataFrame(index=["a", "b", "c"], data={"aa": [1,2,3], "bb": [5,6,7]})
    
    # percentages
    df_perc = df.iloc[:, :].apply(lambda x: x / x.sum(), axis=1) * 100
    
    # plot
    df_perc.plot(ax=ax, kind="barh", stacked=True)
    

    Output plot