Search code examples
pythonplotly

How to add text at barchart, when y is a list, using plotly express


I have the following pandas dataframe

import pandas as pd
foo = pd.DataFrame({'country': {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'},
 'unweighted': {0: 18.0, 1: 16.9, 2: 13.3, 3: 11.3, 4: 13.1},
 'weighted_1': {0: 17.7, 1: 15.8, 2: 14.0, 3: 11.2, 4: 12.8},
 'weighted_2': {0: 17.8, 1: 15.8, 2: 14.0, 3: 11.2, 4: 12.8}})

country unweighted  weighted_1  weighted_2
0   a   18.0    17.7    17.8
1   b   16.9    15.8    15.8
2   c   13.3    14.0    14.0
3   d   11.3    11.2    11.2
4   e   13.1    12.8    12.8

And I am using the following code to produce the barchart using plotly_express

import plotly.express as px
px.bar(
    so,
    x='country',
    y=['unweighted', 'weighted_1', 'weighted_2'],
    barmode='group',
)

enter image description here

I would like to display also the text at each bar. I tried

px.bar(
        so,
        x='country',
        y=['unweighted', 'weighted_1', 'weighted_2'],
        text=['unweighted', 'weighted_1', 'weighted_2'],
        barmode='group',
    )

but it doesnt work.

How could I do that ?


Solution

  • There are automatic annotations in the annotations.

    import plotly.express as px
    px.bar(
        foo,
        x='country',
        y=['unweighted', 'weighted_1', 'weighted_2'],
        text_auto=True,
        barmode='group',
    )
    

    enter image description here