'Folium / Black colored countries
I am trying to visualize some Covid-19 data (Covid Cases Ratio per Country) using folium module. For some reason a few countries appear to be black on the map, for example United States, and I'm using fill_color="YlGn". What is the problem right here?
import os
import folium
import pandas as pd
states = os.path.join('datas', 'countries.geo.json')
country_covid = os.path.join('datas', 'df.csv')
covid_data = pd.read_csv(country_covid, skiprows=0)
bins = list(covid_data['Cases'].quantile([0, 0.25, 0.5, 0.75, 1]))
m = folium.Map(location=[16, 34], zoom_start=5)
folium.Choropleth(
geo_data=states,
name="choropleth",
data=covid_data,
columns=['Country', 'Cases'],
key_on='feature.properties.ADMIN',
fill_color='YlGn',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Covid-19 Rate (%)',
bins=bins,
reset=True
).add_to(m)
folium.LayerControl().add_to(m)
m.save(outfile="map1.html")
Check out a screenshot of the map:
Solution 1:[1]
So the answer is that I had to actually edit the country's name in my geojson file in order to be the same with the equivalent name in the dataframe.
Solution 2:[2]
When a country is "black", it actually means that there is no data for this country.
As I assume you have data for the United States, you should check that the code for the countries in the geojson and the ones in your pandas DataFrame are the same. The error is most likely there, the join between the country data and the geojson can't be made because of different keys.
If you post an exemple of your data and geojson, I could help you more.
Solution 3:[3]
If anyone encounters the same error, one possible solution is the name. In my dataframe the name was marked as United States
and so it is on the folium map.
However, the GeoJSON that can be used is marked United States of America
.
Simply download (or whatever) the JSON and replace United States of America
with United States
.
# just replace United States of America with United States inside the JSON
# url = 'https://raw.githubusercontent.com/python-visualization/folium/master/examples/data/world-countries.json'
url = './world-countries.json'
m = folium.Map(location=[16, 34], zoom_start=5)
folium.Choropleth(
geo_data=url,
name="choropleth",
data=covid_data,
columns=['Country', 'Cases'],
key_on='feature.properties.ADMIN',
fill_color='YlGn',
fill_opacity=0.7,
line_opacity=0.2,
bins=bins,
reset=True
).add_to(m)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | adan |
Solution 3 | Test |