custom fillColor in folium

4.2k Views Asked by At

I want to make custom fillcolor in folium but it kept throwing keyError

KeyError: 'feature.properties.Propinsi'

below is my code

#custom color low value green, high value red
def my_color_function(feature):
    if data_dict['feature.properties.Propinsi'] > 10000:
        return '#ff0000'
    else:
        return '#008000'
    
#define source
jsonmap = 'indonesia-province-simple.json'
indo_province = json.load(open(jsonmap))
df_master = pd.read_csv('covid_provinsi2.csv')
data_dict = df_master.set_index('provinsi')['jumlah_kasus']

#plotting
m = folium.Map(location=[-0.79, 113],tiles='cartodbpositron',zoom_start=4.5)

folium.GeoJson(
    indo_province,
    style_function=lambda feature: {
        'fillColor': my_color_function(feature),
        'color': 'black',
        'weight': 2,
        'dashArray': '5, 5'
    }
).add_to(m)

folium.LayerControl().add_to(m)

m

I'm able to make it work if I don't use geoJson but I can't make custom color. code below running just fine

jsonmap = 'indonesia-province-simple.json'
indo_province = json.load(open(jsonmap))
df_master = pd.read_csv('covid_provinsi2.csv')

m = folium.Map(location=[-0.79, 113],tiles='cartodbpositron',zoom_start=4.5)
folium.Choropleth(
    geo_data=indo_province,
    name='choropleth',
    data=df_master,
    columns=['provinsi', 'jumlah_kasus'],
    key_on='feature.properties.Propinsi',
    fill_color='OrRd',
    legend_name='covid case'
).add_to(m)

folium.LayerControl().add_to(m)

m

here is link to the geoJson data used

here is the link dataset used

I'm sure there's something wrong in my way referencing to geoJson file but I just don't know how to reference it. Any help is appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

I'm not experienced with folium, but I've modified your code to use GeoJson to color-code using the example here. I created a list of region names extracted by the conditional and used 'style_fuction' to conditionalize it.

import pandas as pd
import numpy as np
import folium

#custom color low value green, high value red
# def my_color_function(feature):
#     mask = data_dict['jumlah_kasus'] > 10000
#     mask = ['#ff0000'  if i == 1 else '#008000' for i in mask]
    
#define source
jsonmap = 'indonesia-province-simple.json'
indo_province = json.load(open(jsonmap))
df_master = pd.read_csv('covid_provinsi2.csv')
# data_dict = df_master.set_index('provinsi')['jumlah_kasus']
data_dict = df_master[['provinsi','jumlah_kasus']]
mask = data_dict[data_dict['jumlah_kasus'] > 10000]['provinsi']
mask = mask.tolist()

#plotting
m = folium.Map(location=[-0.79, 113],tiles='cartodbpositron',zoom_start=4.5)

folium.GeoJson(
    indo_province,
    style_function=lambda feature: {
        'fillColor': '#ff0000' if feature['properties']['Propinsi'] in mask else '#008000',
        'color': 'black',
        'weight': 2,
        'dashArray': '5, 5'
    }
).add_to(m)

folium.LayerControl().add_to(m)

m

enter image description here