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

如何控制Plotly Choropleth地图悬停信息仅显示country1字段?

解决Plotly Choropleth悬停仅显示指定内容的问题

要只在悬停时显示country1的内容,移除多余的“Remittances (Million USD) 101 to 300”和“iso alpha=USA”信息,你可以通过自定义hovertemplate实现,核心是用<extra></extra>清空默认的额外信息。以下是修改后的完整代码:

import plotly.express as px
import pandas as pd  # 补充原代码遗漏的pandas导入

df = pd.read_csv('remi_top10.csv')

fig = px.choropleth(df, locations="iso_alpha",
                    color='Remittances (Million USD)', # Remittance Received in July 2022
                    hover_name="country1", # column to add to hover information
                    title= "Remittances Received During the Month of July 2022",
                    color_discrete_sequence=["red", "navy", "blue", "goldenrod", "magenta", "purple", "pink", "green"],
                    width=1000,
                    height=550)

# 自定义悬停模板,只保留country1内容
fig.update_traces(
    hovertemplate='%{hovertext}<extra></extra>'
)

fig.update_layout(
    font_family="Courier New",
    font_color="blue",
    title_font_family="Times New Roman",
    title_font_color="green",
    legend_title_font_color="green"
)
fig.update_xaxes(title_font_family="Arial")
fig.show()

关键说明

  • %{hovertext}对应你设置的hover_name="country1"的内容
  • <extra></extra>会清除Plotly默认追加的额外信息(包括颜色区间、ISO编码等),确保悬停时仅展示国家名称

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 16:48:25