简体   繁体   中英

I'm making a choropleth map with plotly express. How do I match up the values in my csv dataframe with the countries in the geojson file?

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

If the GeoJSON you are using either does not have an id field or you wish you use one of the keys in the properties field, you may use the featureidkey parameter to specify where to match the values of locations.

Bearing that in mind, here's how I fixed my code:

fig = px.choropleth(data_frame=asean10, 
                    geojson=aseangeo, 
                    locations='code', 
                    featureidkey="properties.code",
                    color='Value',
              #     color_continuous_scale="Viridis",
                    range_color=(0, 12))

This is the result I got:

更好的结果

Now I need to fix the colors and positioning, but that's a problem for another day.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM