首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Plotly,更改悬停文本

Plotly,更改悬停文本
EN

Stack Overflow用户
提问于 2020-08-26 13:34:40
回答 1查看 378关注 0票数 1

我正在尝试更改plotly中的悬停文本。举个例子,下面是

代码语言:javascript
复制
import plotly.graph_objects as go

fig = go.Figure(go.Scatter(
    x = [1,2,3,4,5],
    y = [2.02825,1.63728,6.83839,4.8485,4.73463],
    hovertemplate =
    '<i>Price</i>: $%{y:.2f}'+
    '<br><b>X</b>: %{x}<br>'+
    '<b>%{text}</b>',
    text = ['Custom text {}'.format(i + 1) for i in range(5)],
    showlegend = False))

fig.add_trace(go.Scatter(
    x = [1,2,3,4,5],
    y = [3.02825,2.63728,4.83839,3.8485,1.73463],
    hovertemplate = 'Price: %{y:$.2f}<extra></extra>',
    showlegend = False))

fig.update_layout(
    hoverlabel_align = 'right',
    title = "Set hover text with hovertemplate")

fig.show()

你能看到蓝色中写着“轨迹0”的部分吗?这是从哪里来的?如果您将鼠标悬停在红色曲线上,您会注意到类似的东西不会出现。

我想重现这个,所以我想知道它是从哪里来的

EN

回答 1

Stack Overflow用户

发布于 2020-08-26 13:57:32

您可以通过删除/添加trace0, trace1,...标签来启用/禁用每组点的<extra></extra>文本。有关自定义悬停文本的文档可以在here中找到。

要使trace1出现在第二组点上,请删除<extra></extra>标签。

代码语言:javascript
复制
import plotly.graph_objects as go

fig = go.Figure(go.Scatter(
    x = [1,2,3,4,5],
    y = [2.02825,1.63728,6.83839,4.8485,4.73463],
    hovertemplate =
    '<i>Price</i>: $%{y:.2f}'+
    '<br><b>X</b>: %{x}<br>'+
    '<b>%{text}</b>',
    text = ['Custom text {}'.format(i + 1) for i in range(5)],
    showlegend = False))

## Remove the extra tag
fig.add_trace(go.Scatter(
    x = [1,2,3,4,5],
    y = [3.02825,2.63728,4.83839,3.8485,1.73463],
    hovertemplate = 'Price: %{y:$.2f}',
    showlegend = False))

fig.update_layout(
    hoverlabel_align = 'right',
    title = "Set hover text with hovertemplate")

fig.show()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63591099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档