悬停模板不适用于 Plotly Surface 图

问题描述 投票:0回答:0

我偶然发现了 plotly hovertemplates 的一个问题,其中为表面图和其他类型的图(例如热图)创建的文本不同。对于曲面图,hovertemplate 完全失败

虽然对于热图,hovertemplate 可以从 customdata 变量中完美地馈送,就像在文档中所做的那样:

import plotly.graph_objects as go
from plotly.offline import plot as htmly

x = np.round(np.arange(-5,5,0.2),2)
y = np.round(np.arange(0,5,0.2),2)
z = np.matrix([[x[i]**2 + (y[j] - 2.5)**2 for i in range(len(x))] for j in range(len(y))])
mesh = np.meshgrid(x,y)
customdata_ = np.dstack((mesh[0], mesh[1]))

fig = go.Figure()
fig.add_trace(go.Heatmap(
    z=z, customdata=customdata_,
    hovertemplate='<b>z:%{z:.3f}</b><br>x:%{customdata[0]:.3f} <br>y: %{customdata[1]:.3f} '))
fig.update_layout(
    xaxis=dict(title='x', tickmode='array', ticktext=x, tickvals=np.arange(len(x))),
    yaxis=dict(title='y', tickmode='array', ticktext=y, tickvals=np.arange(len(y))),
    font=dict(size=14)
)
htmly(fig)

同样的对曲面图不起作用

fig = go.Figure()
fig.add_trace(go.Surface(
    z=z, customdata=customdata_,
    hovertemplate='<b>z:%{z:.3f}</b><br>x:%{customdata[0]:.3f} <br>y: %{customdata[1]:.3f} '))
fig.update_layout(
    scene=dict(
        xaxis=dict(title='x', tickmode='array', ticktext=x, tickvals=np.arange(len(x))),
        yaxis=dict(title='y', tickmode='array', ticktext=y, tickvals=np.arange(len(y)))
    ),
    font=dict(size=14)
)
htmly(fig)

对于曲面图,只有一组有限的数据点显示悬停模板(具有错误值)。对于其余部分,hovertemplate 仅被解释为文字字符串。

有人设法以类似的方式从悬停模板字段创建正确的悬停文本吗?

python plotly plotly-python surface
© www.soinside.com 2019 - 2024. All rights reserved.