Search code examples
pythoncolorsgeopandasfolium

Column color based when ploting a GeoDataFrame with 'explore' method


I'd like to color the cities that belongs to one hemisphere or another with the color specified at 'Color' column. All the cities of each hemisphere (each category) have the same value for that column. I'd like the legend to show the same colors too.

I'm not being able of getting it. Could anyone help me? Thank you very much in advance.

Note: This is a reproducible example of what I'm dealing with -based on geopandas examples.

import geopandas as gpd

gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
gdf['Hemisphere'] = gdf['geometry'].apply(lambda x: 'Norte'if x.y > 0 else 'Sur')
gdf['Color'] = gdf['Hemisphere'].apply(lambda x: '#D94325' if x=='Norte' else '#5CD925')

gdf.explore(column='Hemisferio', color='Color')

enter image description here


Solution

  • import geopandas as gpd
    import matplotlib.colors as colors
    
    gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
    gdf['Hemisphere'] = gdf['geometry'].apply(lambda x: 'Norte'if x.y > 0 else 'Sur')
    gdf.explore(column='Hemisphere', cmap=colors.ListedColormap(['#D94325','#5CD925']))
    
    

    enter image description here