You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R的Plotly绘图中移除hovertemplate旁的trace0标识

解决Plotly绘图中hovertemplate旁显示"trace0"的问题

你只需要在自定义的hovertemplate末尾加上<extra></extra>标签,就能去掉这个默认显示的trace名称。

原因是Plotly默认会在hover提示框的额外区域显示当前trace的名称(因为你没给trace指定name参数,所以默认显示"trace0"),<extra></extra>可以替换这个默认区域的内容,留空就不会显示任何额外信息了。

修改后的完整代码如下:

library(plotly)

fig <- plot_ly() 
fig <- fig %>%
  add_trace(
    type = 'scatter',
    mode = 'lines+markers',
    x = c(1,2,3,4,5),
    y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
    text = c("Text A", "Text B", "Text C", "Text D", "Text E"),
    hovertemplate = paste('<i>Price</i>: $%{y:.2f}',
                        '<br><b>X</b>: %{x}<br>',
                        '<b>%{text}</b>',
                        '<extra></extra>'),
    showlegend = FALSE
  ) 

内容的提问来源于stack exchange,提问作者firmo23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 08:07:43