Plotly:在 hoverlabel 中显示值,而不是在堆积面积图中显示百分比 `groupnorm='percent'`

Plotly: Show value in hoverlabel instead of percentage in stacked area chart `groupnorm='percent'`

在下图中,悬停标签显示百分比比例值。我宁愿它显示每个大陆在那个特定日期的实际价值。我不确定如何实现它。为清楚起见,

亚洲 9 而不是 37.5,北美 13 而不是 54.16667,欧洲 2 而不是 8.333333(根据下面提供的数据)

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

我尝试直接从数据框中获取值,但 hovertemplate 似乎没有读取 %{calc} 中的任何计算,它只接受直接变量。任何帮助,将不胜感激。使用此代码重现图形-

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')

目标

设置groupnorm='percent'后保持绘图不变,但在hoverinfo中只显示个别值。

回答

  1. df 中为 valuedf['actual] = df['value']
  2. 添加重复的列
  3. px.area()
  4. 中包含hover_data = ['value', 'actual']
  5. fig.update_traces(hovertemplate='%{y}')更改为fig.update_traces(hovertemplate='%{customdata}')

情节 1

详情

您必须包含重复列的原因是 px.area() 会自动计算分配给 px.area(df, x='date', y='value'... 中的 y 的列的百分比。设置 hover_data = ['value', 'actual'] 时,不会对 actual 进行计算,稍后可以通过 fig.update_traces(hovertemplate='%{customdata}').

在 hoverinfo 中访问它

如果您从设置中删除 fig.update_traces(hovertemplate = ...),您将获得以下悬停信息,您可能也会感兴趣:

情节 2

在这种情况下,将 value 更改为 percent 可能更有意义,毕竟显示的是:

情节 3

情节 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()

情节 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()

情节 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()