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

Plotly Choropleth地图:自定义颜色字典无法正确映射颜色

Plotly Choropleth地图:自定义颜色字典无法正确映射颜色

嘿,我看你遇到的问题很典型——自定义颜色没生效反而出现了奇怪的默认色,核心原因是你对Plotly的color_discrete_map参数用法理解错啦!

咱们先拆解下原代码里的问题:
你先给州名映射了颜色,生成了color列(值是'green'/'red'/'gray'),然后在px.choropleth里指定color='color',这时候Plotly是把color列里的这三个字符串当成类别来处理的。但你传的color_discrete_map还是原来的州名-颜色字典,Plotly找不到对应'green'/'red'/'gray'这些类别的颜色映射,就自动用了默认的离散色板,所以才出现了粉色、浅蓝这些奇怪的颜色。

给你两种解决思路,选哪种都行:


思路1:修正color_discrete_map的映射关系

既然你已经生成了color列,那color_discrete_map需要对应color列里的三个类别,直接把每个颜色字符串映射到自身就行:

import geopandas as gpd
import plotly.express as px

# Path to the shapefile of Brazilian states
shapefile_path = 'brasil_estados.json'

# Load the shapefile using geopandas
brasil_mapa = gpd.read_file(shapefile_path, encoding='utf-8')

# Custom color dictionary (red and green)
color_dict = {
    'Minas Gerais': 'green',
    'Tocantins': 'red',
    'Amapá': 'red',
    'Roraima': 'red',
    'Goiás': 'red',
    'Distrito Federal': 'red'
}

# Map colors according to the dictionary
# If the state is not in the dictionary, it will be colored 'gray'
brasil_mapa['color'] = brasil_mapa['name'].map(color_dict).fillna('gray')

# 修正color_discrete_map:对应color列的三个类别
color_discrete_map = {
    'green': 'green',
    'red': 'red',
    'gray': 'gray'
}

# Plot the map using Plotly
fig = px.choropleth(
    brasil_mapa,
    geojson=brasil_mapa.geometry.__geo_interface__,
    locations=brasil_mapa.index,
    color='color',
    color_discrete_map=color_discrete_map,  # 用修正后的映射
    labels={'color': 'State'},
    scope='south america',
    title='Map of Brazilian States with Custom Colors',
    width=1200,
    height=800
)

# Adjusting map visualization
fig.update_geos(fitbounds="locations", visible=False)

# Show the plot
fig.show()

思路2:直接用州名作为颜色类别(更简洁)

其实不需要额外生成color列,直接把州名作为颜色分类,然后在color_discrete_map里把你指定的州映射到对应颜色,再用'default'参数设置未指定州的默认灰色,这样代码更精简:

import geopandas as gpd
import plotly.express as px

# Path to the shapefile of Brazilian states
shapefile_path = 'brasil_estados.json'

# Load the shapefile using geopandas
brasil_mapa = gpd.read_file(shapefile_path, encoding='utf-8')

# Custom color dictionary (red and green)
color_dict = {
    'Minas Gerais': 'green',
    'Tocantins': 'red',
    'Amapá': 'red',
    'Roraima': 'red',
    'Goiás': 'red',
    'Distrito Federal': 'red'
}

# Plot the map using Plotly
fig = px.choropleth(
    brasil_mapa,
    geojson=brasil_mapa.geometry.__geo_interface__,
    locations=brasil_mapa.index,
    color='name',  # 直接用州名作为颜色分类依据
    # 合并自定义颜色和默认灰色
    color_discrete_map={**color_dict, 'default': 'gray'},
    labels={'name': 'State'},
    scope='south america',
    title='Map of Brazilian States with Custom Colors',
    width=1200,
    height=800
)

# Adjusting map visualization
fig.update_geos(fitbounds="locations", visible=False)

# Show the plot
fig.show()

这两种方法都能让你指定的州显示红/绿色,其他州显示灰色,不会再出现奇怪的默认色啦!

备注:内容来源于stack exchange,提问作者Mário Henrique

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 16:24:29