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

如何在Plotly中自定义悬停标签的指标顺序并隐藏经纬度?

解决Plotly Scatter Mapbox悬停标签顺序及经纬度显示问题

方案一:完全移除悬停标签中的经纬度

Plotly的px.scatter_mapbox默认会自动将lat和lon对应的字段加入悬停数据,要隐藏它们,只需在配置中明确排除这两个字段即可。修改代码如下:

@st.cache(hash_funcs={dict: lambda _: None})
def my_stat_map(df_region_map):

    fig_map = px.scatter_mapbox(df_region_map,
                                hover_name='Region name',
                                hover_data=['Confirmed', 'Deaths', 'Recovered',
                                            'Daily confirmed', 'Daily deaths', 'Daily recovered'],
                                exclude=['Latitude dd', 'Longitude dd'],  # 新增排除经纬度的参数
                                lat='Latitude dd',
                                lon='Longitude dd',
                                size='Confirmed',
                                color='Confirmed',
                                color_continuous_scale='Sunsetdark',
                                zoom=3,
                                size_max=45,
                                opacity=0.8,
                                height=600)

    fig_map.update_layout(mapbox_style="carto-positron", showlegend=True)
    fig_map.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})

    dict_map = {'map_key': fig_map}
    return dict_map

也可以用字典形式定义hover_data,直接设置经纬度字段为False:

hover_data={
    'Confirmed': True,
    'Deaths': True,
    'Recovered': True,
    'Daily confirmed': True,
    'Daily deaths': True,
    'Daily recovered': True,
    'Latitude dd': False,
    'Longitude dd': False
}

方案二:将经纬度移至悬停标签末尾

如果需要保留经纬度但调整顺序,最灵活的方式是自定义hovertemplate,完全控制悬停内容的排列逻辑:

@st.cache(hash_funcs={dict: lambda _: None})
def my_stat_map(df_region_map):

    fig_map = px.scatter_mapbox(df_region_map,
                                hover_name='Region name',
                                hover_data=['Confirmed', 'Deaths', 'Recovered',
                                            'Daily confirmed', 'Daily deaths', 'Daily recovered'],
                                lat='Latitude dd',
                                lon='Longitude dd',
                                size='Confirmed',
                                color='Confirmed',
                                color_continuous_scale='Sunsetdark',
                                zoom=3,
                                size_max=45,
                                opacity=0.8,
                                height=600)

    # 自定义悬停模板,按需求排列字段,经纬度放在最后
    fig_map.update_traces(
        hovertemplate='<b>%{hovertext}</b><br>' +
                      'Confirmed: %{customdata[0]}<br>' +
                      'Deaths: %{customdata[1]}<br>' +
                      'Recovered: %{customdata[2]}<br>' +
                      'Daily confirmed: %{customdata[3]}<br>' +
                      'Daily deaths: %{customdata[4]}<br>' +
                      'Daily recovered: %{customdata[5]}<br>' +
                      'Latitude: %{lat}<br>' +
                      'Longitude: %{lon}<br>' +
                      '<extra></extra>'  # 隐藏默认的额外信息(比如trace名称)
    )

    fig_map.update_layout(mapbox_style="carto-positron", showlegend=True)
    fig_map.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})

    dict_map = {'map_key': fig_map}
    return dict_map

这里customdata的索引对应你在hover_data中定义的字段顺序,%{hovertext}对应hover_name设置的地区名称,%{lat}和%{lon}直接调用经纬度数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 17:27:30