In this article, I will explain how to create interactive maps with the folium package in python. Along the way, you will also learn how to apply your new skills to visualize Boston criminal data.
To get started with the task of creating interactive maps with python, I will start with importing all the necessary libraries we need for this task:
import pandas as pd import geopandas as gpd import math import folium from folium import Choropleth, Circle, Marker from folium.plugins import HeatMap, MarkerCluster
Working with interactive maps
I’ll start by creating a relatively simple map with the folium.Map() method:
# Create a map m_1 = folium.Map(location=[42.32,-71.0589], tiles='openstreetmap', zoom_start=10) # Display the map m_1
Several arguments customize the appearance of the map:
- the location defines the initial centre of the map. We use the latitude (42.32 ° N) and longitude (-71.0589 ° E) of the city of Boston.
- the tiles change the style of the map; in this case, we choose the OpenStreetMap style. If you are curious, you can find the other options listed here.
- the zoom_start sets the default zoom of the map, where higher values will zoom closer to the map.
Now take the time to explore by zooming in and out, or dragging the map in different directions.
Working on Data
Now I will add crime data to the map. I won’t focus on the data loading step. Instead, you can imagine that you are at a point where you already have the data in a DataFrame pandas offence. Now let’s have a look at the first 5 rows from the data:
# Load the data crimes = pd.read_csv("crime.csv", encoding='latin-1') # Drop rows with missing locations crimes.dropna(subset=['Lat', 'Long', 'DISTRICT'], inplace=True) # Focus on major crimes in 2018 crimes = crimes[crimes.OFFENSE_CODE_GROUP.isin([ 'Larceny', 'Auto Theft', 'Robbery', 'Larceny From Motor Vehicle', 'Residential Burglary', 'Simple Assault', 'Harassment', 'Ballistics', 'Aggravated Assault', 'Other Burglary', 'Arson', 'Commercial Burglary', 'HOME INVASION', 'Homicide', 'Criminal Harassment', 'Manslaughter'])] crimes = crimes[crimes.YEAR>=2018] # Print the first five rows of the table crimes.head()
To reduce the amount of data we need to fit on the map, we’ll limit our attention to daytime flights:
daytime_robberies = crimes[((crimes.OFFENSE_CODE_GROUP == 'Robbery') & \ (crimes.HOUR.isin(range(9,18))))]
Now we need to add markers to the map with folium.Marker(). Each marker below corresponds to a different flight:
# Create a map m_2 = folium.Map(location=[42.32,-71.0589], tiles='cartodbpositron', zoom_start=13) # Add points to the map for idx, row in daytime_robberies.iterrows(): Marker([row['Lat'], row['Long']]).add_to(m_2) # Display the map m_2
What if we have a lot of markers to add? We need to use folium.plugins.MarkerCluster() that can help declutter the map. Each marker is added to a MarkerCluster object:
# Create the map m_3 = folium.Map(location=[42.32,-71.0589], tiles='cartodbpositron', zoom_start=13) # Add points to the map mc = MarkerCluster() for idx, row in daytime_robberies.iterrows(): if not math.isnan(row['Long']) and not math.isnan(row['Lat']): mc.add_child(Marker([row['Lat'], row['Long']])) m_3.add_child(mc) # Display the map m_3
I hope you liked this article on creating Interactive Maps with Python. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning and Python.