Search code examples
pandasfolium

Colors assosiated with dataframe column in folium


I have a dataframe looking like this:

Lat  |  Long  |  Label
 x1  |   y1   |   id1
 x2  |   y2   |   id2
 x3  |   y3   |   id3

and I want to plot Lat and Long in a folium map, where the markers are colored based on the value of Label. The problem is that Label is a string. If it was an int, I could do the following

m = folium.Map(location=[21.37000, -158.08000], zoom_start=1000)
color_pallete = sns.color_palette()
color_pallete = sns.color_palette("Set2", 8000)
color_pallete = color_pallete.as_hex()

for index, row in test.iterrows():
    c = color_pallete[int(row['label'])]
    folium.CircleMarker([row['Lat'], row['Long']], fill_color=c, radius=2, fill=True, color=c).add_to(m)

Can anyone help me approach this when the Labels are strings? Ie. could I create another column of integers, based on the values of Label? If so, how to do that?

Edit: Just adding color=row['label'] in the args of CircleMarker doesn't help, cause that way everything is a hue of black, not very distinguisable.

Edit2: changed labels to represent uuids


Solution

  • For anyone with the same problem, I used the following to create random color hexes,

    color = "%06x" % random.randint(0, 0xFFFFFF)
    

    as proposed here and then create a dictionary with the labels as keys, and the hexes as keys, to pass to folium.