Moreno
Moreno

Reputation: 638

Plotly express choropleth mexico map not showing

I'm working with the following pandas dataframe:

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}}

And I'm trying to create a choropleth using the following code, nonetheless the map that is displayed is not showing nothing

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()

What I want is a map from Mexico that shows color scale according to the value on percentage column. I'm verynew to plotly so any help would be great

Upvotes: 2

Views: 2227

Answers (2)

Ricardo R Cachu
Ricardo R Cachu

Reputation: 1

It was problems with uppercase, acents and "estado de méxico" in the data downloaded is "méxico" and "ciudad de méxico" is "Ciudad de Mexico"

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: 'michoacán',
      11: 'tamaulipas',
      12: 'méxico',
      13: 'sinaloa',
      14: 'colima',
      15: 'ciudad de méxico',
      16: 'morelos',
      17: 'veracruz',
      18: 'chiapas',
      19: 'mexico',
      20: 'tlaxcala',
      21: 'yucatán',
      22: 'durango',
      23: 'chihuahua',
      24: 'zacatecas',
      25: 'jalisco',
      26: 'coahuila',
      27: 'san luis potosí',
      28: 'aguascalientes',
      29: 'campeche',
      30: 'nuevo leon',
      31: 'querétaro',
      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()

import plotly.graph_objects as go

df = pd.DataFrame({'estado': data['estado'].values(), 'percentage':   data['percentage'].values()})
df['estado'] = df['estado'].str.title()
df['estado'][15]='Ciudad de México'
df['estado'][12]='México'

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()

Upvotes: 0

KRKirov
KRKirov

Reputation: 4004

Your problem turned out to be the fact that the estada names in your data object are not capitalized, whereas they are capitalized in mx_regions_geo. Below, the data object is first converted to a data frame. Then the names of the estada are capitalized.

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()

enter image description here

Example with plotly.graph_objects and 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()

enter image description here

Upvotes: 3

Related Questions