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

使用sf对象创建Plotly图表时无法自定义hoverinfo求助

解决Plotly自定义Hover提示框不显示的问题

你在使用Plotly绘制空间数据时,已指定自定义text内容并设置hoverinfo="text",但hover提示框却显示split参数对应的NUTS_ID值,而非你定义的文本内容。

解决方案

以下两种方法可解决该问题:

方法1:替换hoverinfo为tooltip

Plotly新版本中,tooltip参数对hover内容的控制优先级更高,替换后可强制显示自定义text:

library(plotly)
data(franconia, package = "mapview")

plot_ly(
  franconia,
  split = ~NUTS_ID,
  color = ~district,
  stroke = I("black"),
  text = ~paste("This is text and", NAME_ASCI, "\n is in", district), 
  tooltip = "text",  # 用tooltip替代hoverinfo
  hoveron = "fill"
)

方法2:使用hovertemplate完全自定义提示框

通过hovertemplate直接定义提示框内容,并用<extra></extra>隐藏Plotly默认添加的split字段信息:

library(plotly)
data(franconia, package = "mapview")

plot_ly(
  franconia,
  split = ~NUTS_ID,
  color = ~district,
  stroke = I("black"),
  hovertemplate = ~paste("This is text and", NAME_ASCI, "<br> is in", district, "<extra></extra>"),
  hoveron = "fill"
)

原因说明

当使用split参数拆分图形元素时,Plotly会默认将split字段加入hover提示框,部分版本中hoverinfo的优先级低于该默认行为。使用tooltip或hovertemplate可明确覆盖默认设置,强制显示自定义内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 07:26:03