Python中Choropleth Mapbox绘图异常:无法识别位置、数据不填充
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
Add the missing pandas import (I noticed you used
pd.DataFramebut didn't import pandas—this would throw an error otherwise):import pandas as pdSpecify the
featureidkeyparameter in yourChoroplethmapboxcall. This tells Plotly to look inproperties.id(where you stored the country names) to match against yourdf_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

