使用属性、folium、python 数据框设置颜色
Setting a color using an attribute, folium, python dataframe
我想让CircleMarker使用参数设置颜色,有这种可能吗?我的数据框如下所示:
lon lat segment
ABMF00GLP -61.528 16.262 41
ABPO00MDG 47.229 -19.018 71
ACRG00GHA -0.207 5.641 16
AGGO00ARG -58.140 -34.874 4
AIRA00JPN 130.600 31.824 20
... ... ... ...
YKRO00CIV -5.240 6.871 16
ZAMB00ZMB 28.311 -15.426 90
ZECK00RUS 41.565 43.788 67
ZIM200CHE 7.465 46.877 81
ZIM300CHE 7.465 46.877 81
我可以添加与下面相同颜色的标记,不幸的是我不知道如何让它依赖于段。
for x in df.index:
folium.CircleMarker(list(np.array(df.loc[x])),
popup=x,
radius=3,
color = "red",
).add_to(m)
引入连续色图并在段列中设置最小值和最大值。将段值作为标记颜色设置中的参数。有关颜色映射的 more information,请参阅此内容。
import pandas as pd
import numpy as np
import io
import folium
import branca.colormap as cm
data = '''
id lon lat segment
ABMF00GLP -61.528 16.262 41
ABPO00MDG 47.229 -19.018 71
ACRG00GHA -0.207 5.641 16
AGGO00ARG -58.140 -34.874 4
AIRA00JPN 130.600 31.824 20
YKRO00CIV -5.240 6.871 16
ZAMB00ZMB 28.311 -15.426 90
ZECK00RUS 41.565 43.788 67
ZIM200CHE 7.465 46.877 81
'''
df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
linear = cm.LinearColormap(["green", "yellow", "red"], vmin=df['segment'].min(), vmax=df['segment'].max())
m = folium.Map([df['lat'].mean(), df['lon'].mean()], tiles="cartodbpositron", zoom_start=2)
for _, row in df.iterrows():
folium.CircleMarker([row.lat, row.lon],
popup=row.id,
radius=3,
color = linear(row.segment),
).add_to(m)
m
我想让CircleMarker使用参数设置颜色,有这种可能吗?我的数据框如下所示:
lon lat segment
ABMF00GLP -61.528 16.262 41
ABPO00MDG 47.229 -19.018 71
ACRG00GHA -0.207 5.641 16
AGGO00ARG -58.140 -34.874 4
AIRA00JPN 130.600 31.824 20
... ... ... ...
YKRO00CIV -5.240 6.871 16
ZAMB00ZMB 28.311 -15.426 90
ZECK00RUS 41.565 43.788 67
ZIM200CHE 7.465 46.877 81
ZIM300CHE 7.465 46.877 81
我可以添加与下面相同颜色的标记,不幸的是我不知道如何让它依赖于段。
for x in df.index:
folium.CircleMarker(list(np.array(df.loc[x])),
popup=x,
radius=3,
color = "red",
).add_to(m)
引入连续色图并在段列中设置最小值和最大值。将段值作为标记颜色设置中的参数。有关颜色映射的 more information,请参阅此内容。
import pandas as pd
import numpy as np
import io
import folium
import branca.colormap as cm
data = '''
id lon lat segment
ABMF00GLP -61.528 16.262 41
ABPO00MDG 47.229 -19.018 71
ACRG00GHA -0.207 5.641 16
AGGO00ARG -58.140 -34.874 4
AIRA00JPN 130.600 31.824 20
YKRO00CIV -5.240 6.871 16
ZAMB00ZMB 28.311 -15.426 90
ZECK00RUS 41.565 43.788 67
ZIM200CHE 7.465 46.877 81
'''
df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
linear = cm.LinearColormap(["green", "yellow", "red"], vmin=df['segment'].min(), vmax=df['segment'].max())
m = folium.Map([df['lat'].mean(), df['lon'].mean()], tiles="cartodbpositron", zoom_start=2)
for _, row in df.iterrows():
folium.CircleMarker([row.lat, row.lon],
popup=row.id,
radius=3,
color = linear(row.segment),
).add_to(m)
m