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

Neil picture Neil · Mar 13, 2017 · Viewed 7.8k times · Source

I have pandas dataframe like following

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.

Answer

Bob Haffner picture Bob Haffner · Jun 10, 2017

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

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)

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