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

如何修改Plotly中Choropleth地图的悬停提示框属性?

修改Plotly Choropleth地图悬停提示框的字体大小和尺寸

当然可以实现!在Plotly中,你可以通过配置hoverlabel属性来轻松调整悬停提示框的样式,包括字体大小和提示框的整体尺寸。我结合你的代码给出具体的修改方案:

具体修改方法

1. 针对单个Trace设置(推荐,更灵活)

在你定义的choropleth trace中添加hoverlabel参数,通过它来控制字体大小、内边距(间接调整提示框尺寸)等属性:

import plotly.plotly as py
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')
for col in df.columns:
    df[col] = df[col].astype(str)

scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],\
            [0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]

df['text'] = df['state'] + '<br>' +\
    'Beef '+df['beef']+' Dairy '+df['dairy']+'<br>'+\
    'Fruits '+df['total fruits']+' Veggies ' + df['total veggies']+'<br>'+\
    'Wheat '+df['wheat']+' Corn '+df['corn']

data = [ dict(
        type='choropleth',
        colorscale = scl,
        autocolorscale = False,
        locations = df['code'],
        z = df['total exports'].astype(float),
        locationmode = 'USA-states',
        text = df['text'],
        # 新增的hoverlabel配置
        hoverlabel=dict(
            font=dict(
                size=14,  # 设置提示框内字体大小,按需调整
                family='Arial'  # 可选:设置字体类型
            ),
            bgcolor='rgba(255,255,255,0.9)',  # 可选:设置提示框背景色
            bordercolor='rgb(0,0,0)',  # 可选:设置提示框边框颜色
            padding=dict(
                t=10,  # 顶部内边距
                b=10,  # 底部内边距
                l=15,  # 左侧内边距
                r=15   # 右侧内边距
            )
        ),
        marker = dict(
            line = dict (
                color = 'rgb(255,255,255)',
                width = 2
            )
        ),
        colorbar = dict(
            title = "Millions USD"
        )
    ) ]

layout = dict(
        title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
        geo = dict(
            scope='usa',
            projection=dict( type='albers usa' ),
            showlakes = True,
            lakecolor = 'rgb(255, 255, 255)'),
    )

fig = dict( data=data, layout=layout )
py.iplot( fig, filename='d3-cloropleth-map' )

2. 全局设置(适用于多Trace场景)

如果你希望所有trace的悬停提示框都使用相同样式,可以在layout中添加hoverlabel配置:

layout = dict(
        title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
        geo = dict(
            scope='usa',
            projection=dict( type='albers usa' ),
            showlakes = True,
            lakecolor = 'rgb(255, 255, 255)'),
        # 全局hoverlabel样式
        hoverlabel=dict(
            font=dict(size=14),
            padding=dict(t=10, b=10, l=15, r=15)
        )
    )

参数说明

  • hoverlabel.font.size:直接控制提示框内文字的大小,数值越大字体越醒目。
  • hoverlabel.padding:通过调整上下左右的内边距,间接改变提示框的整体尺寸,内边距越大,提示框看起来越宽敞。
  • 如果你需要进一步控制提示框的宽度,可以在构造df['text']时,通过<br>标签手动换行,或者限制每行的字符数量,让提示框宽度更符合预期。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:51:34