How to add the total in Go.Bar

Hi i have a code like this and I would like to add the total of each bar with hover. How can I do ?

data = [go.Bar(name=col,
                       x=aum_annuel_classe.index.year, 
                       y=aum_annuel_classe[col], 
                       customdata= aum_annuel_classe[col].apply(lambda x: human_format(x,1)),
                       xhoverformat="%Y",
                       xperiodalignment="middle",
                       hovertemplate='<br>'.join(['Année: %{x}',
                                                  'AuM: %{customdata}€'
                                                  ]
                                                 ),
                       marker_color=colors[col]
                       ) 
                for col in aum_annuel_classe.columns]
        fig = go.Figure(data)
        fig.update_layout(barmode='stack',
                          legend=dict(yanchor="top",
                                      y=0.99,
                                      xanchor="left",
                                      x=0.01
                                      ),
                          )
        fig.update_xaxes(ticklabelmode="period",
                         tickformat="%Y"
                         )
        fig.show()

where aum_annuel_classe looks like this:

                       Equity     Bonds      Monetary 
2003-12-31 00:00:00	10,053,046	8,091,899	94,464,474
2004-12-31 00:00:00	26,397,000	11,290,000	70,830,512
2005-12-31 00:00:00	34,987,000	17,366,000	66,598,148
2006-12-31 00:00:00	41,419,226	20,339,366	65,189,570
2007-12-31 00:00:00	43,003,222	31,743,078	60,324,259
2008-12-31 00:00:00	20,216,045	17,841,324	33,548,809
2009-12-31 00:00:00	43,280,214	31,512,790	36,009,135
2010-12-31 00:00:00	67,082,266	40,803,912	41,854,794
2011-12-31 00:00:00	49,501,104	50,544,480	37,833,233
2012-12-31 00:00:00	65,303,268	57,847,712	52,214,552
2013-12-31 00:00:00	93,385,298	51,482,812	59,780,204
2014-12-31 00:00:00	102,966,687.25	59,354,460	54,691,914
2015-12-31 00:00:00	131,998,107	65,488,524	62,950,556
2016-12-31 00:00:00	149,181,157.5	55,219,832	50,493,236
2017-12-31 00:00:00	144,608,622	102,753,976	54,925,120
2018-12-31 00:00:00	59,958,787.75	48,211,628	49,129,320
2019-12-31 00:00:00	62,978,412	46,015,491	47,173,715
2020-12-31 00:00:00	79,725,688	41,304,161	49,824,558
2021-12-31 00:00:00	80,786,912	47,923,468	64,433,973
2022-12-31 00:00:00	52,034,189.5	42,574,610	53,176,219.5
2023-12-31 00:00:00	59,349,198.5	44,487,012	49,463,135.5

Hi jacques,

To get the total of each bar you can do sum on each row of dataframe by using loc function, then save it into a list using list comprehension.

Change your customdata value as a list that have two elements, aum_annuel_classe[col] as first element and list of the total of each bar as second element, then transpose it.

Now you can set the ‘AuM’ value on hovertemplate by using customdata[0] and the total of each bar by using customdata[1].

Based on your code, the change will be something like :

data = [go.Bar(name=col,
               x=aum_annuel_classe.index.year, 
               y=aum_annuel_classe[col], 
               customdata= np.transpose([aum_annuel_classe[col].apply(lambda x: human_format(x,1)), \
                            ["{:0,.2f}".format(aum_annuel_classe.loc[row].sum()) for row in aum_annuel_classe[col].index ]]),
               xhoverformat="%Y",
               xperiodalignment="middle",
               hovertemplate='<br>'.join(['Année: %{x}',
                                          'AuM: %{customdata[0]}€',
                                          'Total: %{customdata[1]}€'
                                          ]
                                         ),
               marker_color=colors[col]
               ) 
        for col in aum_annuel_classe.columns]
fig = go.Figure(data)
fig.update_layout(barmode='stack',
                  legend=dict(yanchor="top",
                              y=0.99,
                              xanchor="left",
                              x=0.01
                              ),
                  )
fig.update_xaxes(ticklabelmode="period",
                 tickformat="%Y"
                 )
fig.show()