4

In the image below, the hoverlabel shows the percent proportionate values. I would rather it show the actual value of each continent on that particular date. I am not sure how to make it happen. For clarity,

Asia would be 9 instead of 37.5, North America 13 instead of 54.16667 and Europe 2 instead of 8.333333 (according to data provided below)

enter image description here

date value continent
2021-01-01 3 Asia
2021-01-02 7 Asia
2021-01-03 9 Asia
2021-01-04 13 Asia
2021-01-01 5 North America
2021-01-02 8 North America
2021-01-03 13 North America
2021-01-04 19 North America
2021-01-01 0 Europe
2021-01-02 0 Europe
2021-01-03 2 Europe
2021-01-04 3 Europe

I tried taking the values directly from the dataframe but hovertemplate does not seem to read any calculations inside the %{calc}, it only accepts direct variables. Any help would be appreciated. Use this code to reproduce the graph-

import pandas as pd
import plotly.express as px

df = pd.DataFrame({'date': list(pd.date_range(start='2021-01-01', periods=4, freq='D')) *3,
                   'value': [3,7,9,13,5,8,13,19,0,0,2,3],
                   'continent':['Asia'] * 4 + ['North America'] * 4 + ['Europe'] * 4})

fig = px.area(df, x='date', y='value', color='continent', groupnorm='percent')
fig.update_traces(hovertemplate='%{y}')
fig.update_layout(hovermode='x unified')
callmeanythingyouwant
  • 1,789
  • 4
  • 15
  • 40

1 Answers1

4

Goal

Keep plot as it is after setting groupnorm='percent', but only display individual values in hoverinfo.

Answer

  1. Include a duplicate column in your df for value with df['actual] = df['value']
  2. Include hover_data = ['value', 'actual'] in px.area()
  3. Change fig.update_traces(hovertemplate='%{y}') to fig.update_traces(hovertemplate='%{customdata}')

Plot 1

enter image description here

Details

The reason why you'll have to include a duplicate column is that px.area() automatically calculates percentages for the column assigned to y in px.area(df, x='date', y='value'.... When setting hover_data = ['value', 'actual'], no calculations are done for actual which is later accesible in the hoverinfor through fig.update_traces(hovertemplate='%{customdata}').

If you drop fig.update_traces(hovertemplate = ...) from your setup, you'll get the following hoverinfo which may also be of interest:

Plot 2

enter image description here

In this case it might make more sense to change value to percent, which after all is displayed:

Plot 3

enter image description here

Complete code for Plot 1

import pandas as pd
import plotly.express as px

df = pd.DataFrame({'date': list(pd.date_range(start='2021-01-01', periods=4, freq='D')) *3,
                   'value': [3,7,9,13,5,8,13,19,0,0,2,3],
                   'continent':['Asia'] * 4 + ['North America'] * 4 + ['Europe'] * 4})

df['actual'] = df['value']

fig = px.area(df, x='date', y='value', color='continent', groupnorm='percent',
              hover_data = ['value', 'actual'])
fig.update_traces(hovertemplate='%{customdata}')


fig.update_layout(hovermode='x unified')
fig.show()

Complete code for Plot 2

import pandas as pd
import plotly.express as px

df = pd.DataFrame({'date': list(pd.date_range(start='2021-01-01', periods=4, freq='D')) *3,
                   'value': [3,7,9,13,5,8,13,19,0,0,2,3],
                   'continent':['Asia'] * 4 + ['North America'] * 4 + ['Europe'] * 4})

df['actual'] = df['value']

fig = px.area(df, x='date', y='value', color='continent', groupnorm='percent',
              hover_data = ['value', 'actual'])
# fig.update_traces(hovertemplate='%{customdata}')
  
fig.update_layout(hovermode='x unified')
fig.show()

Complete code for Plot 3

import pandas as pd
import plotly.express as px

df = pd.DataFrame({'date': list(pd.date_range(start='2021-01-01', periods=4, freq='D')) *3,
                   'percent': [3,7,9,13,5,8,13,19,0,0,2,3],
                   'continent':['Asia'] * 4 + ['North America'] * 4 + ['Europe'] * 4})

df['actual'] = df['percent']

fig = px.area(df, x='date', y='percent', color='continent', groupnorm='percent',
              hover_data = ['percent', 'actual'])
# fig.update_traces(hovertemplate='%{customdata}')
    
fig.update_layout(hovermode='x unified')
fig.show()
vestland
  • 55,229
  • 37
  • 187
  • 305