10

I have a dataframe with latitude, longitude, and power percentage. I want to do something very simple but not sure how: apply a colormap to color the data points based on their percentage. So 90% is red and 100% is blue. I have created both a successful map and colormap, but not sure how to proceed next.

import folium
import pandas as pd
import folium.plugins

import branca
import branca.colormap as cm

data = [
    [33.823400, -118.12194, 99.23],
    [33.823500, -118.12294, 95.23],
    [33.823600, -118.12394, 91.23],
    [33.823700, -118.12494, 90.00]
]

df = pd.DataFrame(data, columns=['latitude','longitude','power'])

x_start = (df['latitude'].max() + df['latitude'].min()) / 2
y_start = (df['longitude'].max() + df['longitude'].min()) / 2
start_coord = (x_start, y_start)

map = folium.Map(location=start_coord, zoom_start=12)

lat = list(df.latitude)
lon = list(df.longitude)

for loc in zip(lat, lon):
    folium.Circle(
        location=loc,
        radius=10,
        #fill=True,
        #color='blue',
        #fill_opacity=0.7
    ).add_to(map)

display(map)

colormap = cm.LinearColormap(colors=['red','lightblue'], index=[90,100],vmin=90,vmax=100)
colormap
geistmate
  • 525
  • 2
  • 5
  • 14

1 Answers1

19

I'm in a rush, but this is how I've done it in the past. Create the CM and then call it like so colormap(.9)

import folium
import pandas as pd
import folium.plugins

import branca
import branca.colormap as cm

data = [
    [33.823400, -118.12194, 99.23],
    [33.823500, -118.12294, 95.23],
    [33.823600, -118.12394, 91.23],
    [33.823700, -118.12494, 90.00]
]

df = pd.DataFrame(data, columns=['latitude','longitude','power'])

x_start = (df['latitude'].max() + df['latitude'].min()) / 2
y_start = (df['longitude'].max() + df['longitude'].min()) / 2
start_coord = (x_start, y_start)


colormap = cm.LinearColormap(colors=['red','lightblue'], index=[90,100],vmin=90,vmax=100)

map = folium.Map(location=start_coord, zoom_start=12)


lat = list(df.latitude)
lon = list(df.longitude)
pow = list(df.power)


for loc, p in zip(zip(lat, lon), pow):
    folium.Circle(
        location=loc,
        radius=10,
        fill=True,
        color=colormap(p),
        #fill_opacity=0.7
    ).add_to(map)

map.add_child(colormap)

display(map)

Map with colour bar

kabdulla
  • 5,199
  • 3
  • 17
  • 30
Bob Haffner
  • 8,235
  • 1
  • 36
  • 43
  • 1
    Revisiting this, I noticed I forgot to mark this as correct. Your solution has helped me out, thank you. – geistmate Jan 27 '20 at 19:51