不确定如何在Folium标记图中使用颜色映射。

11

我有一个数据框包含纬度、经度和功率百分比。我想要做一件非常简单的事情,但不确定如何做:应用一种颜色映射来根据数据点的百分比着色。因此,90% 为红色,100% 为蓝色。我已经成功创建了地图和颜色映射,但不确定下一步该怎么做。

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
1个回答

20

我很匆忙,但这是我过去所做的方式。先创建CM,然后像这样调用它: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


1
重新审视这个问题,我注意到我忘记将其标记为正确答案。你的解决方案帮了我很大的忙,谢谢你。 - geistmate

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接