Search code examples
pythonplotlydata-visualizationplotly-dashplotly-python

Adding percentage of count to a stacked bar chart in plotly


Given the following chart created in plotly. enter image description here

I want to add the percentage values of each count for M and F categories inside each block.

The code used to generate this plot.

arr = np.array([
        ['Dog', 'M'], ['Dog', 'M'], ['Dog', 'F'], ['Dog', 'F'],
        ['Cat', 'F'], ['Cat', 'F'], ['Cat', 'F'], ['Cat', 'M'],
        ['Fox', 'M'], ['Fox', 'M'], ['Fox', 'M'], ['Fox', 'F'],
        ['Dog', 'F'], ['Dog', 'F'], ['Cat', 'F'], ['Dog', 'M']
    ])

df = pd.DataFrame(arr, columns=['A', 'G'])

fig = px.histogram(df, x="A", color='G', barmode="stack")
fig.update_layout(height=400, width=800)

fig.show()

Solution

  • As far as I know histograms in Plotly don't have a text attribute. But you could generate the bar chart yourself and then add the percentage via the text attribute.

    import numpy as np
    import pandas as pd
    import plotly.express as px
    
    arr = np.array([
            ['Dog', 'M'], ['Dog', 'M'], ['Dog', 'F'], ['Dog', 'F'],
            ['Cat', 'F'], ['Cat', 'F'], ['Cat', 'F'], ['Cat', 'M'],
            ['Fox', 'M'], ['Fox', 'M'], ['Fox', 'M'], ['Fox', 'F'],
            ['Dog', 'F'], ['Dog', 'F'], ['Cat', 'F'], ['Dog', 'M']
        ])
    
    df = pd.DataFrame(arr, columns=['A', 'G'])
    
    df_g = df.groupby(['A', 'G']).size().reset_index()
    df_g['percentage'] = df.groupby(['A', 'G']).size().groupby(level=0).apply(lambda x: 100 * x / float(x.sum())).values
    df_g.columns = ['A', 'G', 'Counts', 'Percentage']
    
    px.bar(df_g, x='A', y=['Counts'], color='G', text=df_g['Percentage'].apply(lambda x: '{0:1.2f}%'.format(x)))
    

    enter image description here