Plotly express choropleth 墨西哥地图未显示
Plotly express choropleth mexico map not showing
我正在使用以下 pandas 数据框:
data = {'estado': {0: 'oaxaca',
1: 'nuevo león',
2: 'guerrero',
3: 'hidalgo',
4: 'baja california sur',
5: 'puebla',
6: 'nayarit',
7: 'tabasco',
8: 'baja california',
9: 'quintana roo',
10: 'michoacan de ocampo',
11: 'tamaulipas',
12: 'veracruz de ignacio de la llave',
13: 'sinaloa',
14: 'colima',
15: 'ciudad de mexico',
16: 'morelos',
17: 'veracruz de ignacio',
18: 'chiapas',
19: 'mexico',
20: 'tlaxcala',
21: 'yucatan',
22: 'durango',
23: 'chihuahua',
24: 'zacatecas',
25: 'jalisco',
26: 'coahuila de zaragoza',
27: 'san luis potosi',
28: 'aguascalientes',
29: 'campeche',
30: 'nuevo leon',
31: 'queretaro',
32: 'guanajuato',
33: 'sonora'},
'percentage': {0: 0.34558823529411764,
1: 0.3333333333333333,
2: 0.3218390804597701,
3: 0.30857142857142855,
4: 0.30120481927710846,
5: 0.28860294117647056,
6: 0.2857142857142857,
7: 0.2616033755274262,
8: 0.2576530612244898,
9: 0.2483221476510067,
10: 0.23902439024390243,
11: 0.23595505617977527,
12: 0.23383084577114427,
13: 0.2289855072463768,
14: 0.22727272727272727,
15: 0.22676579925650558,
16: 0.22330097087378642,
17: 0.22186495176848875,
18: 0.21978021978021978,
19: 0.2153928380545163,
20: 0.20689655172413793,
21: 0.1980952380952381,
22: 0.19626168224299065,
23: 0.19240506329113924,
24: 0.1875,
25: 0.1852576647097195,
26: 0.18493150684931506,
27: 0.18250950570342206,
28: 0.18064516129032257,
29: 0.18007662835249041,
30: 0.17862481315396114,
31: 0.1733490566037736,
32: 0.16173570019723865,
33: 0.15902140672782875}}
我正在尝试使用以下代码创建等值线,但显示的地图没有显示任何内容
import plotly.express as px
import requests
repo_url = 'https://raw.githubusercontent.com/angelnmara/geojson/master/mexicoHigh.json'
#Archivo GeoJSON
mx_regions_geo = requests.get(repo_url).json()
fig = px.choropleth(data_frame=data,
geojson=mx_regions_geo,
locations='estado', # nombre de la columna del Dataframe
featureidkey='properties.name', # ruta al campo del archivo GeoJSON con el que se hará la relación (nombre de los estados)
color='percentage', #El color depende de las cantidades
color_continuous_scale="burg", #greens
#scope="north america"
)
fig.update_geos(showcountries=True, showcoastlines=True, showland=True, fitbounds="locations")
fig.show()
我想要的是一张墨西哥地图,它根据百分比列的值显示色标。我是 plotly 的新手,所以任何帮助都会很棒
你的问题原来是你的数据对象中的 estada 名称没有大写,而它们在 mx_regions_geo 中是大写的。下面,首先将数据对象转换为数据框。然后estada的名字大写。
df = pd.DataFrame({'estado': data['estado'].values(), 'percentage': data['percentage'].values()})
Kdf['estado'] = df['estado'].str.capitalize()
fig = px.choropleth(data_frame=df,
geojson=mx_regions_geo,
locations=df['estado'], # nombre de la columna del Dataframe
featureidkey='properties.name', # ruta al campo del archivo GeoJSON con el que se hará la relación (nombre de los estados)
color=df['percentage'], #El color depende de las cantidades
color_continuous_scale="burg",
#scope="north america"
)
fig.update_geos(showcountries=True, showcoastlines=True, showland=True, fitbounds="locations")
fig.show()
使用 plotly.graph_objects 和 Mapbox 的示例。
import plotly.graph_objects as go
fig = go.Figure(go.Choroplethmapbox(name='Mexico', geojson=mx_regions_geo, ids=df['estado'], z=df['percentage'],
locations=df['estado'], featureidkey='properties.name', colorscale='reds',
marker=dict(line=dict(color='black'), opacity=0.6)))
fig.update_layout(mapbox_style='open-street-map',
mapbox_zoom=4,
mapbox_center = {'lat': 25, 'lon': -99}
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
我正在使用以下 pandas 数据框:
data = {'estado': {0: 'oaxaca',
1: 'nuevo león',
2: 'guerrero',
3: 'hidalgo',
4: 'baja california sur',
5: 'puebla',
6: 'nayarit',
7: 'tabasco',
8: 'baja california',
9: 'quintana roo',
10: 'michoacan de ocampo',
11: 'tamaulipas',
12: 'veracruz de ignacio de la llave',
13: 'sinaloa',
14: 'colima',
15: 'ciudad de mexico',
16: 'morelos',
17: 'veracruz de ignacio',
18: 'chiapas',
19: 'mexico',
20: 'tlaxcala',
21: 'yucatan',
22: 'durango',
23: 'chihuahua',
24: 'zacatecas',
25: 'jalisco',
26: 'coahuila de zaragoza',
27: 'san luis potosi',
28: 'aguascalientes',
29: 'campeche',
30: 'nuevo leon',
31: 'queretaro',
32: 'guanajuato',
33: 'sonora'},
'percentage': {0: 0.34558823529411764,
1: 0.3333333333333333,
2: 0.3218390804597701,
3: 0.30857142857142855,
4: 0.30120481927710846,
5: 0.28860294117647056,
6: 0.2857142857142857,
7: 0.2616033755274262,
8: 0.2576530612244898,
9: 0.2483221476510067,
10: 0.23902439024390243,
11: 0.23595505617977527,
12: 0.23383084577114427,
13: 0.2289855072463768,
14: 0.22727272727272727,
15: 0.22676579925650558,
16: 0.22330097087378642,
17: 0.22186495176848875,
18: 0.21978021978021978,
19: 0.2153928380545163,
20: 0.20689655172413793,
21: 0.1980952380952381,
22: 0.19626168224299065,
23: 0.19240506329113924,
24: 0.1875,
25: 0.1852576647097195,
26: 0.18493150684931506,
27: 0.18250950570342206,
28: 0.18064516129032257,
29: 0.18007662835249041,
30: 0.17862481315396114,
31: 0.1733490566037736,
32: 0.16173570019723865,
33: 0.15902140672782875}}
我正在尝试使用以下代码创建等值线,但显示的地图没有显示任何内容
import plotly.express as px
import requests
repo_url = 'https://raw.githubusercontent.com/angelnmara/geojson/master/mexicoHigh.json'
#Archivo GeoJSON
mx_regions_geo = requests.get(repo_url).json()
fig = px.choropleth(data_frame=data,
geojson=mx_regions_geo,
locations='estado', # nombre de la columna del Dataframe
featureidkey='properties.name', # ruta al campo del archivo GeoJSON con el que se hará la relación (nombre de los estados)
color='percentage', #El color depende de las cantidades
color_continuous_scale="burg", #greens
#scope="north america"
)
fig.update_geos(showcountries=True, showcoastlines=True, showland=True, fitbounds="locations")
fig.show()
我想要的是一张墨西哥地图,它根据百分比列的值显示色标。我是 plotly 的新手,所以任何帮助都会很棒
你的问题原来是你的数据对象中的 estada 名称没有大写,而它们在 mx_regions_geo 中是大写的。下面,首先将数据对象转换为数据框。然后estada的名字大写。
df = pd.DataFrame({'estado': data['estado'].values(), 'percentage': data['percentage'].values()})
Kdf['estado'] = df['estado'].str.capitalize()
fig = px.choropleth(data_frame=df,
geojson=mx_regions_geo,
locations=df['estado'], # nombre de la columna del Dataframe
featureidkey='properties.name', # ruta al campo del archivo GeoJSON con el que se hará la relación (nombre de los estados)
color=df['percentage'], #El color depende de las cantidades
color_continuous_scale="burg",
#scope="north america"
)
fig.update_geos(showcountries=True, showcoastlines=True, showland=True, fitbounds="locations")
fig.show()
使用 plotly.graph_objects 和 Mapbox 的示例。
import plotly.graph_objects as go
fig = go.Figure(go.Choroplethmapbox(name='Mexico', geojson=mx_regions_geo, ids=df['estado'], z=df['percentage'],
locations=df['estado'], featureidkey='properties.name', colorscale='reds',
marker=dict(line=dict(color='black'), opacity=0.6)))
fig.update_layout(mapbox_style='open-street-map',
mapbox_zoom=4,
mapbox_center = {'lat': 25, 'lon': -99}
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()