Michael
Michael

Reputation: 1

Folium map with markers of different colors: 'name not defined' error

I want to add markers of different color, indicating the cluster, to a choropleth map. Intersting thing, I did this before, but the code is not working anymore. I would like to use the matplotlib colormap 'rainbow', but I receive the error message 'name 'rainbow' not defined'. Error message indicates first occurrence of rainbow in the code. I import matplotlib.cm and matplotlib.colors, so the colormaps should be there. I am totally confused by this error.

Here is my code:

import pandas as pd
import matplotlib.cm as cm
import matplotlib.colors as colors
import folium
from geopy.geocoders import Nominatim

gdp_bins = df['GDP'].quantile([0, 0.25, 0.5, 0.75, 1])

gdp_map = folium.Map([latitude, longitude], zoom_start=8)

folium.Choropleth(
    geo_data=geojson,
    name ='Choropleth',
    data = df,
    columns=["Code", "GDP"],
    key_on="feature.properties.HASC_1",
    fill_color="Blues",
    fill_opacity=0.7,
    line_opacity=0.5,
    legend_name="GDP",
    bins=gdp_bins,
    reset=True).add_to(gdp_map)

# add markers in different colors per cluster to the map   
markers_colors = []
for lat, lon, poi, cluster in zip(df['Lat'], df['Lon'], df['Canton'], df['Cluster Labels']):
    label = folium.Popup(str(poi) + ' Cluster ' + str(cluster), parse_html=True)
    folium.CircleMarker(
        [lat, lon],
        radius=5,
        popup=label,
        color=rainbow[cluster-1],
        fill=True,
        fill_color=rainbow[cluster-1],
        fill_opacity=0.7).add_to(gdp_map)
    
gdp_map'''

Upvotes: 0

Views: 1321

Answers (1)

RJ Adriaansen
RJ Adriaansen

Reputation: 9619

You should either import rainbow directly:

from matplotlib.cm import rainbow

or if you import cm (from matplotlib import cm), format your code as such:

color=cm.rainbow[cluster-1],
fill=True,
fill_color=cm.rainbow[cluster-1],

Upvotes: 1

Related Questions