pandas 如何通过一些标签从大叶地图组上的熊猫数据框中绘制纬度和经度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42756934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:10:32  来源:igfitidea点击:

how to plot lat and long from pandas dataframe on folium map group by some labels

pandasfolium

提问by Neil

I have pandas dataframe like following

我有如下所示的Pandas数据框

Latitude          Longitude          Class
 40.7145           -73.9425            A
 40.7947           -73.9667            B
 40.7388           -74.0018            A
 40.7539           -73.9677            B

I want to plot above on folium map which will also display class associated with lat and long.
I am using following code.

我想在上面绘制大叶地图,该地图还将显示与纬度和经度相关的类。
我正在使用以下代码。

import folium

map_osm = folium.Map(location=[40.742, -73.956])
train_df.apply(lambda row:folium.CircleMarker(location=[row["Latitude"], 
                                                  row["Longitude"]]).add_to(map_osm),
     axis=1)

How to plot and display class as well so that on map its easier to understand Class wise distribution of points.

如何绘制和显示类别,以便在地图上更容易理解点的类别分布。

回答by Bob Haffner

You could change the fill color of the CircleMarkers. Maybe add some popups or perhaps label them

您可以更改 CircleMarkers 的填充颜色。也许添加一些弹出窗口或标记它们

train_df
   Latitude  Longitude Class
0   40.7145   -73.9425     A
1   40.7947   -73.9667     B
2   40.7388   -74.0018     A
3   40.7539   -73.9677     B

Using colors to distinguish class with a simple dict

使用颜色通过简单的字典来区分类别

colors = {'A' : 'red', 'B' : 'blue'}

map_osm = folium.Map(location=[40.742, -73.956], zoom_start=11)

train_df.apply(lambda row:folium.CircleMarker(location=[row["Latitude"], row["Longitude"]], 
                                              radius=10, fill_color=colors[row['Class']])
                                             .add_to(map_osm), axis=1)

map_osm

enter image description here

在此处输入图片说明

Using colors and popups

使用颜色和弹出窗口

colors = {'A' : 'red', 'B' : 'blue'}

map_osm = folium.Map(location=[40.742, -73.956], zoom_start=11)

train_df.apply(lambda row:folium.CircleMarker(location=[row["Latitude"], row["Longitude"]], 
                                              radius=10, fill_color=colors[row['Class']], popup=row['Class'])
                                             .add_to(map_osm), axis=1)

map_osm

enter image description here

在此处输入图片说明

Using colors and 'labels' using DivIcon. Switched to using iterrows() and a for loop since we are creating CircleMarkers and Markers (for the labels)

使用 DivIcon 使用颜色和“标签”。切换到使用 iterrows() 和 for 循环,因为我们正在创建 CircleMarkers 和 Markers(用于标签)

from folium.features import DivIcon

colors = {'A' : 'red', 'B' : 'blue'}


map_osm = folium.Map(location=[40.742, -73.956], zoom_start=11)

for _, row in train_df.iterrows():
    folium.CircleMarker(location=[row["Latitude"], row["Longitude"]], 
                                radius=5, fill_color=colors[row['Class']]).add_to(map_osm)

    folium.Marker(location=[row["Latitude"], row["Longitude"]], icon=DivIcon(icon_size=(150,36), icon_anchor=(0,0),
        html='<div style="font-size: 16pt; color : {}">{}</div>'.format(colors[row['Class']], 
                                                                        row['Class']))).add_to(map_osm)


map_osm

enter image description here

在此处输入图片说明