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

Plotly ScatterGeo文本颜色优化:如何提升地图文本对比度?

解决Plotly Scattergeo文本标签对比度低的问题

你可以通过以下几种方式修改fig.add_scattergeo生成的文本轨迹,提升可读性:

1. 固定文本颜色

直接为文本设置高对比度的固定颜色(比如白色或黑色),这是最简单的解决方案:

fig.add_scattergeo(
    locations=top_sectors_empl["st"],
    locationmode="USA-states",
    text=top_sectors_empl["short_sector"],
    mode="text",
    # 白色字体适配深色背景,若背景偏浅可改为"black"
    textfont=dict(color="white", size=10)
)

2. 添加文本描边

给文本添加描边,即使颜色和背景接近,也能清晰区分:

fig.add_scattergeo(
    locations=top_sectors_empl["st"],
    locationmode="USA-states",
    text=top_sectors_empl["short_sector"],
    mode="text",
    textfont=dict(color="white", size=10),
    # 添加黑色描边增强辨识度
    textoutline="black",
    textoutlinewidth=1,
    textposition="middle center"
)

3. 动态匹配文本颜色(根据背景深浅)

如果想让文本颜色自动适配每个州的背景颜色(深色背景用白色,浅色背景用黑色),可以通过计算颜色亮度实现:

from plotly.colors import label_rgb

# 计算颜色亮度(YIQ色彩空间公式)
def get_contrast_text_color(rgb_color):
    r, g, b = label_rgb(rgb_color)
    # 亮度阈值128区分深浅背景
    brightness = ((r * 299) + (g * 587) + (b * 114)) / 1000
    return "black" if brightness > 128 else "white"

# 获取Viridis色阶的颜色列表
viridis_colors = px.colors.sequential.Viridis
# 归一化数据,匹配色阶位置
values = top_sectors_empl["ds_state_sector_headcount"]
min_val, max_val = values.min(), values.max()
normalized_vals = (values - min_val) / (max_val - min_val)

# 生成每个州对应的文本颜色
text_colors = []
for val in normalized_vals:
    color_idx = int(val * (len(viridis_colors) - 1))
    bg_color = viridis_colors[color_idx]
    text_colors.append(get_contrast_text_color(bg_color))

# 添加带动态颜色的文本标签
fig.add_scattergeo(
    locations=top_sectors_empl["st"],
    locationmode="USA-states",
    text=top_sectors_empl["short_sector"],
    mode="text",
    textfont=dict(size=10, color=text_colors)
)

修改后的完整代码示例(固定颜色+描边版本)

import plotly.express as px
import os

# Create a choropleth map with state names
fig = px.choropleth(
    top_sectors_empl,
    locations="st",  # Column with state names
    locationmode="USA-states",  # Set location mode for U.S. states
    color="ds_state_sector_headcount",  # Column with data to visualize
    scope="usa",  # Restrict to the U.S.
    title="Highest Employing Sector by State",  # Title for the map
    color_continuous_scale="Viridis",  # Color scale
    # labels={"value": "Value"},  # Label for the legend
)
fig.update_coloraxes(
    dict(
        colorbar=dict(
            title="Annual Salary",
        )
    )
)
fig.add_scattergeo(
    locations=top_sectors_empl["st"],
    locationmode="USA-states",
    text=top_sectors_empl["short_sector"],
    mode="text",
    textfont=dict(color="white", size=10),
    textoutline="black",
    textoutlinewidth=1
)

# Save the figure as an HTML file
fig.write_html("choropleth_map_empl.html")

os.system("open choropleth_map_empl.html")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 07:45:18