52

I am using folium to create a choropleth map of a set of countries. I am following the documentation. However. for some reason the map doesn't show any shades. I am using the world geojson from natural earth (see the gist).

My dataframe looks like:

>>> spatial_scores.head()

Out[1]:
id  Country Score
PER Peru    2.810300
HND Honduras    2.734521
GUF French Guiana   2.730886
SLV El Salvador 2.473134
CRI Costa Rica  2.454963

The world geojson looks like:

>>> world_json['features'][0]['id']

Out [2]:
u'AFG'

The relevant portions of the choropleth codes are as below:

map1 = folium.Map(location=[-15., -60], zoom_start=4)

map1.geo_json(geo_path=world_json_path,
              data_out='data.json',
              data=spatial_scores,
              columns=['id', 'Score'],
              threshold_scale=[0, 1, 2, 3, 4],
              key_on='features.id',
              fill_color='BuPu', fill_opacity=0.7, line_opacity=0.5,
              legend_name='Score')

map1.create_map('./Scores.html')

However, am not getting any choropleth result and left with just the base country map as below Chorpleth Output

Is there something I am doing wrong?

[Edit]

I figured out the problem. To plot the choropleth I needed to keep only those keys in the geojson which were also in my data frame.

merged = gdf.merge(spatial_scores, left_on='name', right_on='Country')
spatial_gdf = gpd.GeoDataFrame(merged.iloc[:, [0, 1]])
data_df = merged.iloc[:, [2, 3, 4]]
7
  • same here.. did you find the solution? Commented Feb 7, 2016 at 5:18
  • 2
    hi I actually solved it myself.. Will update the gist tonight. The dead simple answer is that in the geojson you need to put only those keys for which the data is available in the corresponding table Commented Feb 9, 2016 at 21:23
  • Great, thanks :) Looking forward to seeing your code! Commented Feb 10, 2016 at 6:24
  • 3
    If this library is giving you trouble, you could try Plotly instead. It also supports choropleth plots and hovering for exact data. Commented Aug 18, 2020 at 2:59
  • 2
    @goofd, go ahead and write an answer to your own question -- that way it can be closed out Commented Oct 6, 2021 at 18:27

1 Answer 1

1

To plot the choropleth OP needed to keep only those keys in the geojson which were also in the data frame.

merged = gdf.merge(spatial_scores, left_on='name', right_on='Country')
spatial_gdf = gpd.GeoDataFrame(merged.iloc[:, [0, 1]])
data_df = merged.iloc[:, [2, 3, 4]]
Sign up to request clarification or add additional context in comments.

1 Comment

@goofd I was looking at the unaswered questions in the Python tag and this was the top question. I found it a bit silly for the top unanswered question to have an answer that was added in 2016 so I looked at the meta and followed their advice by simply adding your answer as a community answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.