1
votes

I'm trying to create a chloropleth chart using plotly express. I have two files, my geojson file and my data file. Example snippet for one country in my geojson file below:

{'type': 'Feature',
 'properties': {'ADMIN': 'Aruba', 'ISO_A3': 'ABW'},
 'geometry': {'type': 'Polygon',
  'coordinates': [[[-69.99693762899992, 12.577582098000036],
    [-69.93639075399994, 12.53172435100005],
    [-69.92467200399994, 12.519232489000046],
    [-69.91576087099992, 12.497015692000076],
    [-69.88019771999984, 12.453558661000045],
    [-69.87682044199994, 12.427394924000097],
    [-69.88809160099993, 12.417669989000046],
    [-69.90880286399994, 12.417792059000107],
    [-69.93053137899989, 12.425970770000035],
    [-69.94513912699992, 12.44037506700009],
    [-69.92467200399994, 12.44037506700009],
    [-69.92467200399994, 12.447211005000014],
    [-69.95856686099992, 12.463202216000099],
    [-70.02765865799992, 12.522935289000088],
    [-70.04808508999989, 12.53115469000008],
    [-70.05809485599988, 12.537176825000088],
    [-70.06240800699987, 12.546820380000057],
    [-70.06037350199995, 12.556952216000113],
    [-70.0510961579999, 12.574042059000064],
    [-70.04873613199993, 12.583726304000024],
    [-70.05264238199993, 12.600002346000053],
    [-70.05964107999992, 12.614243882000054],
    [-70.06110592399997, 12.625392971000068],
    [-70.04873613199993, 12.632147528000104],
    [-70.00715084499987, 12.5855166690001],
    [-69.99693762899992, 12.577582098000036]]]},
 'id': 'ABW'}

Head from df is shown below which has the column 'data' which will be used to create the heatmap

Location_Site Country City Cluster_Name Market_Type data id
2 IT-MIL Italy Milan Italy Mature 73.14% ITA
3 ES-MAD Spain Madrid Iberia Mature 55.27% ESP
4 PT-LIS Portugal Lisbon Iberia Medium 45.71% PRT
5 AE-DXB United Arab Emirates Dubai EMEA Emerging Markets (EEM) Emerging 62.98% ARE
6 EG-CAI Egypt Cairo EMEA Emerging Markets (EEM) Emerging 20.36% EGY

The below code snippet is what I'm trying to execute to plot my choropleth graph

fig = px.choropleth(df,
                    locations = 'id',
                    geojson = data, 
                    color = 'data')
fig.show()

I am receiving the below error after execution:

ValueError: The first argument to the plotly.graph_objs.layout.Template 
constructor must be a dict or 
an instance of :class:`plotly.graph_objs.layout.Template`

Any ideas on what might be creating this error? Thanks!

1

1 Answers

0
votes

To solve your problem, you need to tie the ID value of the data frame to the ISO_A3 value of the geojson value. aruba was modified to ABW for ITA in Italy, and the output of the map was obtained.

import plotly.express as px

geo_data = {'type': 'Feature',
 'properties': {'ADMIN': 'Aruba', 'ISO_A3': 'ABW'},
 'geometry': {'type': 'Polygon',
  'coordinates': [[[-69.99693762899992, 12.577582098000036],
    [-69.93639075399994, 12.53172435100005],
    [-69.92467200399994, 12.519232489000046],
    [-69.91576087099992, 12.497015692000076],
    [-69.88019771999984, 12.453558661000045],
    [-69.87682044199994, 12.427394924000097],
    [-69.88809160099993, 12.417669989000046],
    [-69.90880286399994, 12.417792059000107],
    [-69.93053137899989, 12.425970770000035],
    [-69.94513912699992, 12.44037506700009],
    [-69.92467200399994, 12.44037506700009],
    [-69.92467200399994, 12.447211005000014],
    [-69.95856686099992, 12.463202216000099],
    [-70.02765865799992, 12.522935289000088],
    [-70.04808508999989, 12.53115469000008],
    [-70.05809485599988, 12.537176825000088],
    [-70.06240800699987, 12.546820380000057],
    [-70.06037350199995, 12.556952216000113],
    [-70.0510961579999, 12.574042059000064],
    [-70.04873613199993, 12.583726304000024],
    [-70.05264238199993, 12.600002346000053],
    [-70.05964107999992, 12.614243882000054],
    [-70.06110592399997, 12.625392971000068],
    [-70.04873613199993, 12.632147528000104],
    [-70.00715084499987, 12.5855166690001],
    [-69.99693762899992, 12.577582098000036]]]},
 'id': 'ABW'}

import pandas as pd
import numpy as np
import io

data = '''
 Location_Site Country City Cluster_Name Market_Type data id 
2 IT-MIL Italy Milan Italy Mature 73.14% ABW 
3 ES-MAD Spain Madrid Iberia Mature 55.27% ESP 
4 PT-LIS Portugal Lisbon Iberia Medium 45.71% PRT 
5 AE-DXB "United Arab Emirates" Dubai "EMEA Emerging Markets (EEM)" Emerging 62.98% ARE 
6 EG-CAI Egypt Cairo "EMEA Emerging Markets (EEM)" Emerging 20.36% EGY 
'''

df = pd.read_csv(io.StringIO(data), delim_whitespace=True)

fig = px.choropleth(df,
                    locations = 'id',
                    geojson = geo_data, 
                    featureidkey="properties.ISO_A3",
                    color = 'data')
fig.show()

enter image description here