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

Python中Choropleth Mapbox绘图异常:无法识别位置、数据不填充

Fixing Blank Choropleth Mapbox for European Countries in Python

Hey, let's figure out why your map is showing up blank even though the color scale works—this is a common matching issue with Plotly's Choroplethmapbox!

The Core Problem

You forgot to tell Plotly which field in your GeoJSON to match against your DataFrame's country names. By default, Plotly looks for a root-level id field in the GeoJSON features, but you stored your matching value in properties.id (via your loop), and didn't specify that path in the plot parameters. Without this, Plotly can't link your country data to the map shapes, hence the blank map.

Step-by-Step Fixes

  1. Add the missing pandas import (I noticed you used pd.DataFrame but didn't import pandas—this would throw an error otherwise):

    import pandas as pd
    
  2. Specify the featureidkey parameter in your Choroplethmapbox call. This tells Plotly to look in properties.id (where you stored the country names) to match against your df_map['Country'] values.

Corrected Full Code

import urllib.request
from urllib.request import urlopen
import json
import pandas as pd
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go

# Fetch European GeoJSON and add id property
url_eu="https://raw.githubusercontent.com/leakyMirror/map-of-europe/master/GeoJSON/europe.geojson"
with urlopen(url_eu) as response_eu:
    eu_countries = json.load(response_eu)
for i in range(0,len(eu_countries['features'])):
    eu_countries['features'][i]['properties']['id'] = eu_countries['features'][i]['properties']['NAME']

# Create map data
data = [['France', 10], ['Germany', 22], ['Italy', 5], ['Poland',7], ['Spain',8], ['United Kingdom',21]]
df_map = pd.DataFrame(data, columns = ['Country', 'count'])

# Updated plot code with featureidkey
fig = go.Figure(go.Choroplethmapbox(
    geojson=eu_countries,
    locations=df_map['Country'],
    z=df_map['count'],
    featureidkey="properties.id",  # Critical line to link data and map shapes
    colorscale='matter',
    zmin=0,
    colorbar_title = "Amazon warehouses",
    marker_opacity=0.5,
    marker_line_width=0.2
))
fig.update_layout(mapbox_style="carto-positron", mapbox_zoom=3, mapbox_center = {"lat": 50 , "lon": 5})
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

Even Simplified Version

You don't even need the loop to add the properties.id field—you can directly use the original NAME property from the GeoJSON by setting featureidkey="properties.NAME". This cuts out unnecessary code:

# Skip the loop that adds properties.id, then update the plot call:
fig = go.Figure(go.Choroplethmapbox(
    geojson=eu_countries,
    locations=df_map['Country'],
    z=df_map['count'],
    featureidkey="properties.NAME",  # Use original NAME field directly
    colorscale='matter',
    zmin=0,
    colorbar_title = "Amazon warehouses",
    marker_opacity=0.5,
    marker_line_width=0.2
))

Just double-check that the country names in your DataFrame exactly match the NAME values in the GeoJSON (e.g., "United Kingdom" vs. any abbreviations like "UK"—the GeoJSON you're using does use "United Kingdom", so your current data is fine).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 09:02:52