In [1]:
! pip install folium
In [1]:
import folium
import pandas as pd
import random
In [2]:
# we need to center the map in the middle of the US. I googled for the location.
CENTER_US = (39.8333333,-98.585522)
london = (51.5074, -0.1278)
map = folium.Map(location=CENTER_US, zoom_start=4)
map
Out[2]:
In [3]:
# read in a data file of IP address to locations.
data = pd.read_csv('https://raw.githubusercontent.com/mafudge/datasets/master/clickstream/ip_lookup.csv')
data.sample(5)
Out[3]:
In [5]:
# Let's place each location on the map
for row in data.to_records():
pos = (row['ApproxLat'],row['ApproxLng'])
marker = folium.Marker(location=pos,
popup="%s, %s" % (row['City'],row['State'])
)
map.add_child(marker)
map
Out[5]:
In [7]:
# Same thing with a different icon and colors. Icons come from http://fontawesome.io/icons/ but its an older version.
colors = ['red', 'blue', 'green', 'purple', 'orange', 'darkred',
'lightred', 'beige', 'darkblue', 'darkgreen', 'cadetblue',
'darkpurple', 'pink', 'lightblue', 'lightgreen',
'gray', 'black', 'lightgray']
for row in data.to_records():
pos = (row['ApproxLat'],row['ApproxLng'])
marker = folium.Marker(location=pos,
popup="%s, %s" % (row['City'],row['State']),
icon = folium.Icon(color = random.choice(colors), icon='user')
)
map.add_child(marker)
map
Out[7]:
In [12]:
# There are other map tiles available. See https://folium.readthedocs.io/en/latest/quickstart.html
# Instead of Markers we use circles colors are HTML color codes http://htmlcolorcodes.com/
CENTER_US = (39.8333333,-98.585522)
map2 = folium.Map(location=CENTER_US, zoom_start=4)
for row in data.to_records():
map2.add_child(folium.CircleMarker(location=(row['ApproxLat'],row['ApproxLng']),
popup=row['City'], radius=10, color='#0000FF', fill_color='#FF3333'))
map2
Out[12]:
In [13]:
# State level geo-json overlay choropleth
CENTER_US = (39.8333333,-98.585522)
state_geojson = 'WMC3-us-states.json'
map3 = folium.Map(location=CENTER_US, zoom_start=4, tiles=' Open Street Map')
map3.choropleth(geo_path=state_geojson)
map3
In [16]:
states = pd.read_csv('https://raw.githubusercontent.com/jasonong/List-of-US-States/master/states.csv')
state_counts = pd.DataFrame( {'Counts' : data['State']. value_counts() } ).sort_index()
state_counts['StateCode'] = state_counts.index
state_data = states.merge(state_counts, how="left", left_on='Abbreviation', right_on='StateCode')
state_data = state_data[['Abbreviation','Counts']]
state_data = state_data.fillna(0)
state_data
Out[16]:
In [25]:
CENTER_US = (39.8333333,-98.585522)
state_geojson = 'WMC3-us-states.json'
map3 = folium.Map(location=CENTER_US, zoom_start=4, tiles=' Open Street Map')
folium.Choropleth(geo_data=state_geojson,data=state_data, columns=['Abbreviation','Counts'],
key_on ='feature.id', fill_color='BuGn', legend_name='Website Visitors').add_to(map3)
map3
Out[25]:
In [19]:
In [23]:
# Here's a more straigtforward example with unemployment data:
unemployment = pd.read_csv('https://raw.githubusercontent.com/wrobstory/vincent/master/examples/data/US_Unemployment_Oct2012.csv')
state_geojson = 'WMC3-us-states.json'
map4 = folium.Map(location=CENTER_US, zoom_start=4, tiles=' Open Street Map')
folium.Choropleth(geo_data=state_geojson,data=unemployment,
columns=['State','Unemployment'], key_on ='feature.id', fill_color='YlGn',
legend_name='2012 US Unemployment Rate %').add_to(map4)
map4
Out[23]:
In [ ]: