Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot Latitude longitude points from dataframe on folium map - iPython

I have a dataframe with lat/lon coordinates

latlon
(51.249443914705175, -0.13878830247011467)
(51.249443914705175, -0.13878830247011467)
(51.249768239976866, -2.8610415615063034)
...

I would like to plot these on to a Folium map but I'm not sure of how to iterate through each of the rows.

any help would be appreciated, Thanks in advance!

like image 938
hsquared Avatar asked Sep 08 '16 23:09

hsquared


2 Answers

This can solve your issue

import folium
mapit = None
latlon = [ (51.249443914705175, -0.13878830247011467), (51.249443914705175, -0.13878830247011467), (51.249768239976866, -2.8610415615063034)]
for coord in latlon:
    mapit = folium.Map( location=[ coord[0], coord[1] ] )

mapit.save( 'map.html')

Edit (using marker)

import folium
latlon = [ (51.249443914705175, -0.13878830247011467), (51.249443914705175, -0.13878830247011467), (51.249768239976866, -2.8610415615063034)]
mapit = folium.Map( location=[52.667989, -1.464582], zoom_start=6 )
for coord in latlon:
    folium.Marker( location=[ coord[0], coord[1] ], fill_color='#43d9de', radius=8 ).add_to( mapit )

mapit.save( 'map.html')

It'd be great if you use this reference: https://github.com/python-visualization/folium

like image 77
stuartnox Avatar answered Nov 03 '22 05:11

stuartnox


Below is how I did it, I'm actually trying to put together a notebook of examples (adding color, popup, etc.). I'm still working out the kinks but you can find it here:

https://github.com/collinreinking/longitude_latitude_dot_plots_in_python_with_folium

import folium
import pandas as pd

#create a map
this_map = folium.Map(prefer_canvas=True)

def plotDot(point):
    '''input: series that contains a numeric named latitude and a numeric named longitude
    this function creates a CircleMarker and adds it to your this_map'''
    folium.CircleMarker(location=[point.latitude, point.longitude],
                        radius=2,
                        weight=5).add_to(this_map)

#use df.apply(,axis=1) to "iterate" through every row in your dataframe
data.apply(plotDot, axis = 1)


#Set the zoom to the maximum possible
this_map.fit_bounds(this_map.get_bounds())

#Save the map to an HTML file
this_map.save('html_map_output/simple_dot_plot.html')

this_map
like image 22
Collin Reinking Avatar answered Nov 03 '22 05:11

Collin Reinking