Search code examples
pythonbar-chartpercentagestacked

How to represents stacked bar plot integer values in percentage


Help please,

A, B and C are columns

X , Y , Z, Q , R are my index values

I want to make a stacked bar plot in python (matplot lib) which calculates the normalized - percentage values for each OUT_CAUSE per market and represents it in the chart.

 OUT_CAUSE  A   B   C   
 MARKET                                             
 X  12.0    11.0    4.0
 Y  9.0     12.0    3.0
 Z  16.0    40.0    1.0
 Q  23.0    19.0    3.0
 R  17.0    15.0    2.0

X axis of the plot should be MARKET Y axis of the plot should be % contribution of OUT_CAUSE for each market.

Sum of the % should be equal to 100


I was using this for representation of direct numbers

df(kind='bar', stacked=True ,figsize=(8, 8));


Solution

  • You can have a loop to create %age for all columns:

    for colname in df.columns: 
        colsum = sum(df[colname])
        df[colname+'%'] = round(100*df[colname]/colsum,1)  # create new column for %ages
    

    Output:

          A     B    C    A%    B%    C%
    X  12.0  11.0  4.0  15.6  11.3  30.8
    Y   9.0  12.0  3.0  11.7  12.4  23.1
    Z  16.0  40.0  1.0  20.8  41.2   7.7
    Q  23.0  19.0  3.0  29.9  19.6  23.1
    R  17.0  15.0  2.0  22.1  15.5  15.4
    

    You can then use following command to create your stacked bar chart:

    df.iloc[:,3:].T.plot.bar(stacked=True, rot=0)
    plt.show()
    

    enter image description here

    If you cannot count the number of columns, you can filter out columns names by presence of % character:

    colnames = list(filter(lambda x: '%' in x, df.columns.tolist()))
    df[colnames].T.plot.bar(stacked=True, rot=0)
    plt.show()