简体   繁体   English

Plotly:跟踪名称出现在 HoverInfo 之外

[英]Plotly: Trace Name Appears Outside of HoverInfo

Below is an example of some code where name appears on the outside of the hoverinfo label.下面是一些代码示例,其中名称出现在 hoverinfo label 的外部。

In my actual code I've already accounted for the name inside of the hoverinfo so I just want to make this bit disappear (see highlighted section in picture below).在我的实际代码中,我已经考虑了 hoverinfo 中的名称,所以我只想让这个位消失(见下图中突出显示的部分)。

Unfortunately I can't set name ="" because I'm using the name value to change the colour of the markers.不幸的是,我无法设置name =""因为我使用 name 值来更改标记的颜色。

Is anyone able to help with this please or has had any experience with trying to do this before?有没有人能帮忙解决这个问题,或者以前有过尝试这样做的经验? Any help would be massively appreciated.任何帮助将不胜感激。

It's strange because this is shown to happen on the plotly dash official documentation on this page .这很奇怪,因为这显示在此页面上的 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()

在此处输入图像描述

Try adding <extra></extra> at the end of the hovertemplate definition.尝试在hovertemplate定义的末尾添加<extra></extra> As explained in the Plotly documentation this should remove the trace name.Plotly 文档中所述,这应该删除跟踪名称。

fig_ex.update_traces(hovertemplate = "x: %{x} <br> y: %{y} <extra></extra>")

You can also pass hoverlabel = {'namelength': -1} , as described here and as hinted at for JavaScript here .您还可以传递hoverlabel = {'namelength': -1} ,如此处所述以及此处JavaScript所暗示的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM