Reputation: 217
I would like to display additional data while hovering over a curve created by go.Scatter. With the script below correct x and y values are shown in the popup, but x^2 and cos are always shown as NaN. I would be very appreciative for any help.
import dash
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objects as go
import numpy as np
x = np.mgrid[0.0:10.0:100j]
y = np.sin(x)
fig = go.Figure()
fig.add_trace(go.Scatter(x = x, y = y, line_width = 4,
customdata = [x**2, np.cos(x)],
hovertemplate = "<br>".join([
"x = %{x:,.1f}",
"y = %{y:,.1f}",
"x^2 = %{customdata[0]:,.1f}",
"cos = %{customdata[1]:,.1f}"
])
))
app = dash.Dash()
app.layout = html.Div([dcc.Graph(figure=fig)])
app.run_server()
Upvotes: 1
Views: 744
Reputation: 21
For anyone else struggling with this when using static labels or other data points :
customdata array length must match the length of the input dataset.
For instance for variables wd, dc and a dataframe df :
customdata = np.array([[int(wd),int(dc)] for x in np.arange(len(df))])
Upvotes: 0
Reputation: 1280
import plotly.graph_objects as go
import numpy as np
x = np.mgrid[0.0:10.0:100j]
y = np.sin(x)
custom_data = np.stack((x**2, np.cos(x)), axis=-1)
fig = go.Figure()
fig.add_trace(go.Scatter(x = x, y = y, line_width = 4))
fig.update_traces(customdata=custom_data,
hovertemplate ="x: %{x}<br>"+\
"y: %{y}<br>"+\
"x**2: %{customdata[0]: .1f}<br>"+\
"cos: %{customdata[1]: .1f}")
fig.show()
Upvotes: 2