Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set color for missing values in folium choropleth

I have a dataframe with some countries and variables and I would like to produce a choropleth map with folium, using a geojson file for the entire world. I have a problem with folium assigning maximum value on a color scale to countries that are not present in my dataframe. Minimum exaple below:

import random
import pandas as pd
import folium
import json

map_data = pd.DataFrame({
    'A3':['POL', 'CZE', 'SVK', 'HUN', 'AUT'],
    'value':random.sample(range(10), 5)
})

m = folium.Map(
    location = [50, 15], 
    zoom_start = 4
)

m.choropleth(
    geo_data = 'https://github.com/simonepri/geo-maps/releases/download/v0.6.0/countries-land-10km.geo.json',
    data = map_data,
    columns = ['A3', 'value'],
    key_on = 'feature.properties.A3',
    fill_color = 'YlOrRd'
)

enter image description here

My question is the following: How can I tell folium to assign a specific color (e.g., gray or transparent) to missing countries (i.e., those present in json file but not in map_data), instead of coloring them as a maximum value for given variable (which is a strange behavior)?

like image 957
pieca Avatar asked Aug 22 '18 07:08

pieca


People also ask

What is Geo_data parameter in Folium Choropleth map?

geo_data (string/object) — URL, file path, or data (json, dict, geopandas, etc) to your GeoJSON geometries. data (Pandas DataFrame or Series, default None) — Data to bind to the GeoJSON. columns (dict or tuple, default None) — If the data is a Pandas DataFrame, the columns of data to be bound.

What other colors are available in the Folium library?

You can use: ['red', 'blue', 'green', 'purple', 'orange', 'darkred', 'lightred', 'beige', 'darkblue', 'darkgreen', 'cadetblue', 'darkpurple', 'white', 'pink', 'lightblue', 'lightgreen', 'gray', 'black', 'lightgray'] icon_color (str, default 'white') – The color of the drawing on the marker.

How do you make a Choropleth map in Folium?

To create a choropleth map using folium, we need to first initiate a base map by using folium. Map() and then add layers to it. We can pass the starting coordinates to the map by using the location parameter. The starting coordinates we choose here (40,-96) approximately represent the center of the U.S. map.

What is Key_on in Folium?

geometries” (Folium documentation). No matter how we load the file we must convert the geometry data to function properly with this method. The key_on parameter of this method binds the data for each specific location (GeoJSON data) with the data for that location (i.e. population).


2 Answers

It seems there in no way to achieve that with choropleth method. I found a workaround with custom style_function and GeoJson instead of using choropleth:

import random
import pandas as pd
import folium
from branca.colormap import LinearColormap
import json

map_data = pd.DataFrame({
    'A3':['POL', 'CZE', 'SVK', 'HUN', 'AUT'],
    'value':random.sample(range(10), 5)
})

map_dict = map_data.set_index('A3')['value'].to_dict()

color_scale = LinearColormap(['yellow','red'], vmin = min(map_dict.values()), vmax = max(map_dict.values()))

def get_color(feature):
    value = map_dict.get(feature['properties']['A3'])
    if value is None:
        return '#8c8c8c' # MISSING -> gray
    else:
        return color_scale(value)

m = folium.Map(
    location = [50, 15], 
    zoom_start = 4
)

folium.GeoJson(
    data = 'https://github.com/simonepri/geo-maps/releases/download/v0.6.0/countries-land-10km.geo.json',
    style_function = lambda feature: {
        'fillColor': get_color(feature),
        'fillOpacity': 0.7,
        'color' : 'black',
        'weight' : 1,
    }    
).add_to(m)

enter image description here

like image 200
pieca Avatar answered Oct 07 '22 08:10

pieca


This has been fixed in folium in a recent pull request: https://github.com/python-visualization/folium/pull/1005

If you install folium from git (or from PyPY once the 0.7 version is released) you can use the nan_fill_color and nan_fill_opacity arguments of the choropleth method of Map to style elements without a value.

The final example in this Notebook shows how to do that: https://nbviewer.jupyter.org/github/python-visualization/folium/blob/master/examples/GeoJSON_and_choropleth.ipynb#Using-choropleth-method

like image 41
Conengmo Avatar answered Oct 07 '22 07:10

Conengmo