I’m trying to format hover text and include additional data with customdata
or hover_data
.
The issue is I do not know in advance the shape of the additional data.
Actually, I found 2 ways of doing this:
1) Plotly Express
fig = px.line(
ef,
x=ef["Risk"],
y=ef["Return"],
hover_data=ef.columns[3:], # DataFrame with assets weights
title=f"Efficient Frontier",
)
This approach seems to be the most straightforward. The additional data inherits labels form the original DataFrame (not only values).
However I can’t edit hovertemplate
here. And I didn’t find a way to control the hover_data floats format.
- go.Figure
weights_array = np.stack([ef[n] for n in ef.columns[3:]], axis=-1)
fig = go.Figure(
data=go.Scatter(
x=ef["Risk"],
y=ef["Return"],
customdata=weights_array,
hovertemplate=('<b>Risk: %{x:.2f}% <br>Return: %{y:.2}%</b><BR><BR>weights:<BR>' +
'%{customdata}%' +
'<extra></extra>'),
mode="lines",
name=f"Efficient Frontier",
)
)
It seems to be more flexible approach as hovertemplate can be managed here. However I still miss required features:
- no way to get data labels
-
%{customdata:.2f}%
does not work … no way to set float formats again
Do I miss something? Is there any other way to handle additional data with variable shape? Is there a way to set format for floats for the whole Hovertemplate?