Search code examples
pythonmapsgeopandasfolium

How to deal with extreme values on a Folium map


I have very little experience with folium maps. I need to make a map with the number of establishments in each department, the problem is that the capital has far more establishments than the interior, so when I create the color layer I get the capital as dark blue and all the rest with the same lighter color. This way the map is not so useful... How could I solve that? I thought of maybe dividing the value by the population but it would be better to use the original value. In the documentation, I did not find a way to parameterize the color.

    df1 = pd.DataFrame({'code':['75','77','78','91','92','93','94','95'],'value':['13000','2000','2500','2300','2150','2600','1630','1300']})
    dep_geo = geopandas.read_file('./dep.json', driver="JSON") #geodata taken from https://github.com/gregoiredavid/france-geojson/blob/master/departements.geojson
departments = {'75', '77', '78', '91', '92', '93', '94', '95'}
dep_geo = dep_geo[dep_geo['code'].isin(departments)]

df_map = dep_geo.merge(df1, how="left", left_on=['code'], right_on=['code'])

my_map = folium.Map(location=[48.856614, 2.3522219],
                    zoom_start = 9, tiles='cartodbpositron')

folium.Choropleth(
geo_data=df_map,
data=df_map,
columns=['code',"value"],
key_on="feature.properties.code",
fill_color='YlGnBu',
fill_opacity=0.5,
line_opacity=0.2,
legend_name="value ",
smooth_factor=0,
Highlight= True,
line_color = "black",
name = "value",
show=False,
overlay=True,
nan_fill_color = "White"
).add_to(my_map)

Result: enter image description here

Thank you for your help!


Solution

    • it's as simple as using vmax argument. I've set to 85th percentile
    • also used geopandas explore() to generate the folium map
    import geopandas as gpd
    import pandas as pd
    import folium
    
    df1 = pd.DataFrame(
        {
            "code": ["75", "77", "78", "91", "92", "93", "94", "95"],
            "value": ["13000", "2000", "2500", "2300", "2150", "2600", "1630", "1300"],
        }
    )
    # dep_geo = geopandas.read_file('./dep.json', driver="JSON") #geodata taken from https://github.com/gregoiredavid/france-geojson/blob/master/departements.geojson
    dep_geo = gpd.read_file(
        "https://github.com/gregoiredavid/france-geojson/raw/master/departements.geojson"
    )  # geodata taken from https://github.com/gregoiredavid/france-geojson/blob/master/departements.geojson
    
    
    departments = {"75", "77", "78", "91", "92", "93", "94", "95"}
    dep_geo = dep_geo[dep_geo["code"].isin(departments)]
    df_map = dep_geo.merge(df1, how="left", left_on=["code"], right_on=["code"])
    df_map["value"] = pd.to_numeric(df_map["value"])
    
    df_map.explore(
        column="value",
        cmap="YlGnBu",
        vmax=df_map["value"].quantile(0.85),
        style_kwds=dict(
            color="rgba(0,0,0,.2)",
        ),
        location=[48.856614, 2.3522219],
        zoom_start=9,
        tiles="cartodbpositron",
    )
    

    enter image description here