1

我想在将鼠标悬停在 go.Scatter 创建的曲线上时显示其他数据。使用下面的脚本,弹出窗口中会显示正确的 x 和 y 值,但 x^2 和 cos 始终显示为 NaN。我会非常感激任何帮助。

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()
4

1 回答 1

1
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()

示例输出图

于 2021-06-18T21:09:30.663 回答