Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Plotly express choropleth墨西哥地图未显示_Python_Pandas_Plotly Python - Fatal编程技术网

Python Plotly express choropleth墨西哥地图未显示

Python Plotly express choropleth墨西哥地图未显示,python,pandas,plotly-python,Python,Pandas,Plotly Python,我正在使用以下数据帧: 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:

我正在使用以下数据帧:

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}}
我试图用下面的代码创建一个choropleth,但是显示的地图没有显示任何内容

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_对象和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()

哇!如此简单,我永远不会意识到。非常感谢。