![](/img/trans.png)
[英]Python Plotly xaxis hover text disappears when hoverinfo is set in a trace
[英]Plotly: Trace Name Appears Outside of HoverInfo
下面是一些代码示例,其中名称出现在 hoverinfo label 的外部。
在我的实际代码中,我已经考虑了 hoverinfo 中的名称,所以我只想让这个位消失(见下图中突出显示的部分)。
不幸的是,我无法设置name =""
因为我使用 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()
尝试在hovertemplate
定义的末尾添加<extra></extra>
。 如Plotly 文档中所述,这应该删除跟踪名称。
fig_ex.update_traces(hovertemplate = "x: %{x} <br> y: %{y} <extra></extra>")
您还可以传递hoverlabel = {'namelength': -1}
,如此处所述以及此处JavaScript所暗示的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.