Folium – Plot GeoJSON Polygon with Custom Fill Color

choroplethfoliumgeopandaspython

I have polygons with lat/long values associated with identifiers in a GeoDataFrame as shown below. Consider an example with two identifiers A and B, polygon A has three points and B has four points, their lat/long values are as shown below. Corresponding to each point (lat/long), I also have an associated numeric value as shown in the last column.

id    geometry                                                                         values
A   POLYGON((lat_A_1 long_A_1, lat_A_2 long_A_2, lat_A_3 long_A_3))                    10,12,13
B   POLYGON((lat_B_1 long_B_1, lat_B_2 long_B_2, lat_B_3 long_B_3, lat_B_4 long_B_4))  4,8,16,20

I iterate over the GeoDataFrame and plot these polygons on the map using this code

geo_j = folium.GeoJson(data=geo_j,
                       style_function={ 
                           'fillColor': 'blue'
                       })

Is there a way that I can fill the polygon with a custom colormap based on the column values in the GeoDataFrame, such as red for 0-5, blue for 6-10 and green for 11-20. How can this be done?

Best Answer

this is the solution that I came up with:

import matplotlib
import geopandas as gpd
import matplotlib
import matplotlib.pyplot as plt

# gdf: One's own geopandas geoDataFrame
# colname: name of the gdf to be plotted into the Folium Map

xmin, ymin, xmax, ymax = gdf.total_bounds

centroidx = np.mean([xmin, xmax])
centroidy = np.mean([ymin, ymax])

map1 = folium.Map(
    location=[centroidy, centroidx],
    tiles='cartodbpositron',
    zoom_start=6,
)

cmap = matplotlib.cm.get_cmap('viridis')

vmin = gdf[colname].min()
vmax = gdf[colname].max()


norm = matplotlib.colors.SymLogNorm(vmin=vmin, vmax=vmax, linthresh=0.1)

def fetchHexFromValue(value):
  NormedValue = norm(value)
  RGBAValue = cmap(NormedValue)
  HEXValue = matplotlib.colors.to_hex(RGBAValue)
  return HEXValue



for idx, r in gdf.iterrows():

    lat = r["geometry"].centroid.y
    lon = r["geometry"].centroid.x
    folium.Marker(location=[lat, lon],
                  popup='idx:{0} <br> {1}: {2}'.format(idx,
                                                       colname, 
                                                       r[colname])
    ).add_to(map1)

gdf.explore(colname, cmap="viridis", m=map1)

map1
Related Question