Beatriz Santos
Beatriz Santos

Reputation: 175

Creating a folium map with markers with different colors

I'm using folium to plot a map based on lat and lng. My dataframe is something like this, where every lat and lng as an attribute "Circuito":

Circuito Latitude Longitude    
L2RC 41.36394 -8.550200
L21M 41.22638 -8.693360
LBXP 41.15796 -8.610030
L2RC 41.36394 -8.550200
LERM 41.23865 -8.531550
LCAN 41.14016 -8.634990
LARE 41.19195 -8.556460
LCAR 41.05805 -8.563920
LBXP 41.15786 -8.600700
LBAG 41.18931 -8.526040

I would like to plot the color of the markers based on the attribute "Circuito". But I have only found examples where there is a small number of different colors to plot, which is not the case. For now I have something like this:

for i in range(len(df)):
   if df.loc[i,'Circuito']==L2RC
    icon=folium.Icon(color='white')
    l2rc_group.add_child(folium.Marker((df[i, 'Latitude'],df[i, 'Longitude']), icon=icon))

The problem with this solution is that I have to make an if condition for each possible "circuito" attribute (and there are a lot) and I have to write the color to plot. I was looking for a solution where this isn't needed but I don't know if it is possible.

Thank you!

Upvotes: 2

Views: 14844

Answers (1)

Captain Caveman
Captain Caveman

Reputation: 1526

Edit: Can you use different icons rather than standard markers? If so, try:

# list of available icons
help(folium.Icon)

# implement:
icon_color = ["#"+''.join([random.choice('ABCDEF0123456789') for i in range(6)])]
icon = folium.Icon(icon="globe", icon_color=icon_color, color="lightgreen", prefix="fa")

This method will generate a random hexadecimal color and assign it to the icon for each Circuito.

import random
import pandas as pd

for i in range(len(df)):
    icon_color = ["#"+''.join([random.choice('ABCDEF0123456789') for i in range(6)])] 
    icon=folium.Icon(color=icon_color)
    l2rc_group.add_child(folium.Marker((df[i, 'Latitude'],df[i, 'Longitude']), icon=icon))

Edit: Unfortunately, after reading the Folium docs, it looks like they limit you to 19 colors.

Here is another approach using a list of the available colors. Given that you need 62 different colors, this isn't sufficient. Maybe use some colors twice but change the outline color of the marker to differentiate it or use a different icon for each and the limited color palette.

marker_colors = [
    'red',
    'blue',
    'gray',
    'darkred',
    'lightred',
    'orange',
    'beige',
    'green',
    'darkgreen',
    'lightgreen',
    'darkblue',
    'lightblue',
    'purple',
    'darkpurple',
    'pink',
    'cadetblue',
    'lightgray',
    'black'
]

for i in range(len(df)):
    for color in marker_colors:
        icon=folium.Icon(color=color)
        l2rc_group.add_child(folium.Marker((df[i, 'Latitude'],df[i, 'Longitude']), icon=icon))

Folium marker color options for Markers: https://python-visualization.github.io/folium/modules.html#folium.map.Icon.color_options

A possible work around using hexadecimal codes: Numbers in map marker in Folium

Upvotes: 3

Related Questions