下面是一些代码的示例,其中名称出现在悬停信息标签的外部。
在我的实际代码中,我已经考虑了悬停信息中的名称,所以我只想让这一点消失(请参见下图中突出显示的部分)。
不幸的是,我无法设置name ="",因为我正在使用名称值来更改标记的颜色。
有谁能帮忙解决这个问题,或者以前有过尝试这样做的经验吗?任何帮助将不胜感激。
这很奇怪,因为这在本页的plotly dash 官方文档中显示发生了。
category = "a"
from plotly.subplots import make_subplots
fig_ex = make_subplots(rows=1, cols=2)
fig_ex.add_scatter(y=[4, 2, 3.5], mode="markers",
marker=dict(size=20, color="MediumPurple"),
name="a", row=1, col=1)
fig_ex.add_bar(y=[2, 1, 3],
marker=dict(color="MediumPurple"),
name="b", row=1, col=1)
fig_ex.add_scatter(y=[2, 3.5, 4], mode="markers",
marker=dict(size=20, color="MediumPurple"),
name="c", row=1, col=2)
fig_ex.add_bar(y=[1, 3, 2],
marker=dict(color="MediumPurple"),
name="d", row=1, col=2)
fig_ex.update_layout(showlegend = False)
fig_ex.update_traces(hovertemplate =
"x: %{x}<br>" +
"y: %{y}")
fig_ex.for_each_trace(
lambda trace: trace.update(marker_color="LightSeaGreen") if trace.name == category else (),
)
fig_ex.show()
Run Code Online (Sandbox Code Playgroud)
<extra></extra>尝试在定义末尾添加hovertemplate。正如Plotly 文档中所解释的,这应该删除跟踪名称。
fig_ex.update_traces(hovertemplate = "x: %{x} <br> y: %{y} <extra></extra>")
Run Code Online (Sandbox Code Playgroud)