TL;DR - When I execute the px.choropleth() function, it just gives me a big map of Asia. None of the countries are colored in. What went wrong?
In more detail:
I started off with a csv file containing GDP for all countries in the world and a geojson file with all the countries in Asia. I had no trouble using pandas to subset the data for ASEAN countries from the csv file.
One of my first challenges was figuring out how to remove all non-ASEAN countries from the geojson file. I learned about a package called geopandas and used it to subset ASEAN countries and then convert the geopandas dataframe back to a geojson file for use in the plotly express choropleth function. I suspect the way I used geopandas to subset could be the source of the problem but I am not sure.
Here is a glimpse of what my subsetted dataframe (for GDP values) looks like:
Country Year Value code
11390 Brunei 2018 1.356691e+10 BRN
13585 Cambodia 2018 2.457175e+10 KHM
37026 Indonesia 2018 1.042173e+12 IDN
My understanding is that the geojson file needs to have ID's for each country that correspond with the countries in the dataframe. So when I was subsetting the geojson file with geopandas, I appended a new 'code' column straight from my csv dataframe. I checked and it does seem to have worked. I made the geopandas dataframe into a geojson file, the only countries left are ASEAN countries and each has the correct code ('BRN', 'KHM' etc) in its properties. I named this subsetted geojson file 'asean.json.'
I tried using this code to fill the locations paramater in the plotly choropleth function. It didn't work.
Here is my plotly function:
path_to_file = 'asean.json'
with open(path_to_file) as f:
aseangeo = geojson.load(f)
fig = px.choropleth(data_frame=asean10,
geojson=aseangeo,
locations='code',
color='Value',
# color_continuous_scale="Viridis",
range_color=(0, 12))
fig.show()
The results are disappointing, all I got was a big map of the entire continent of Asia with none of the countries filled in.
Like I said earlier, I suspect it could be the way I'm using geopandas to subset the geojson file, but I'm just not sure. What went wrong?
So for anyone looking at this post with the same problem, the answer lies in this link provided by r-beginners.
https://plotly.com/python/choropleth-maps/
Under the Indexing by Geojson properties section, it clearly says
Bearing that in mind, here's how I fixed my code:
This is the result I got:
Now I need to fix the colors and positioning, but that's a problem for another day.