使用属性、folium、python 数据框设置颜色

发布于 2025-01-11 11:37:23 字数 771 浏览 0 评论 0原文

我希望 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)

I would like CircleMarker to set the color using an argument, is there such a possibility? I have dataframe something like this below:

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   

I can add markers with the same color like below, unfortunately I don't know how to make it segment dependent.

for x in df.index:
    folium.CircleMarker(list(np.array(df.loc[x])),
                      popup=x,
                      radius=3,
                      color = "red",
                      ).add_to(m)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向日葵 2025-01-18 11:37:23

引入连续颜色图并在段列中设置最小值和最大值。将段值作为标记颜色设置中的参数。有关颜色图的更多信息,请参阅此内容。

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

输入图片此处描述

Introduce a continuous colormap and set minimum and maximum values in segment columns. Takes a segment value as an argument in the marker color setting. See this for more information about colormaps.

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

enter image description here

乖乖 2025-01-18 11:37:23

Geopandas 具有非常有用的交互式地图。当您在 geopandas 数据帧上调用此方法时,它将返回 folium 地图实例。

要使用基于 segment 值的标记颜色,只需执行以下操作:

df.explore(
     column='segment')

请注意,您的数据框必须是 geopandas 数据框,而不仅仅是 pandas 数据框。

Geopandas has very useful interactive mapping. When you call this method on geopandas dataframe, it will return folium map instance.

For you to use color the markers based on the segment value, simply do:

df.explore(
     column='segment')

Please note that you dataframe has to be geopandas dataframe, not just pandas dataframe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文