Making a map with Python
Making maps with Python¶
import os
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import geopandas as gp
import pyproj
Method 1: Folium (based upon Leaflet.js)¶
import folium
m=folium.Map(
location=[36.5236, -87.6750],
tiles='Stamen Toner',
zoom_start=4
)
m
Now let's change the basemap and add some vector layers
m=folium.Map(
location=[36.5236, -87.6750],
tiles='Mapbox Bright',
zoom_start=4
)
folium.Marker([45.3288, -100.6625], popup='<i>I"m a marker!</i>').add_to(m)
folium.CircleMarker(
location=[37.5215, -112.6261],
radius=50,
popup='I"m a circle',
color='#3186cc',
fill=True,
fill_color='#3186cc'
).add_to(m)
m
Using Geopandas
While Folium looks nice, I tend to prefer using Geopandas. Being built upon Pandas means it's lighting fast and the syntax is similar to Pandas. So you can easily manipulate fields and process data quickly.
import matplotlib.pyplot as plt
import pandas as pd
import geopandas as gp
usa=gp.read_file('united-states/us-albers-counties.json')
plt.style.use('fivethirtyeight')
It's really easy to plot data using Geopandas.
usa.plot()
Okay so we may need to clean this up some. Like doing away with the pesky axis, enlarging the map and creating a choropleth map.
f, ax = plt.subplots(1, figsize=(15, 10))
ax.set_title("US counties")
ax.set_axis_off()
plt.axis('equal');
usa.plot(ax=ax)
plt.show()
Before proceeding let's take a quick look at our data.
usa.head()
unemployment=pd.read_csv('unemployment.csv')
unemployment.head()
Now I'm going to merge our two datasets together to create a choropleth map.
usa['FIPStxt']=pd.to_numeric(usa['fips'].str[1:])
#=pd.merge(unemployment, usa, how='right', on='FIPStxt')
US_jobs = usa.merge(unemployment, on='FIPStxt')
Let's go ahead and plot everything to make sure the merge was successful.
f, ax = plt.subplots(1, figsize=(15, 10))
ax.set_title("US unemployment rate 2016 by counties" , fontsize=17)
ax.set_axis_off()
plt.axis('equal')
US_jobs.plot( column='Unemployment_rate_2016' ,cmap='OrRd', ax=ax, edgecolor='grey')
plt.show()
So it turns out that not everything made it through the merge. So let's pick a state that made it through the merge, focus on that and do some number crunching. I'm going to go with California.
CA_jobs=US_jobs.loc[US_jobs['iso_3166_2'] == 'CA']
f, ax = plt.subplots(1, figsize=(15, 10))
ax.set_title("California unemployment rate 2016 by counties", fontsize=17)
ax.set_axis_off()
plt.axis('equal')
CA_jobs.plot( column='Unemployment_rate_2016' ,cmap='OrRd', ax=ax, edgecolor='grey')
plt.show()
Great! Let's dig a little further and do some graphing with Matplotlib. First I'm going to list all the columns so we know which column headers to pick.
list(CA_jobs.columns.values)
Now I'm going to make a time series plot for all unemployment rates from 2007 to 2016 for all the California counties. Given that there are 58 of them I'll forgo adding county labels.
f, ax = plt.subplots(1, figsize=(20, 25))
ax.set_title("California unemployment time series by counties", fontsize=20)
CA_jobs2 = CA_jobs.filter(regex='Unemployment_rate')
plt.plot(pd.Series(list(range(2007,2017))), CA_jobs2.T)
plt.show()
Well, this is for now the end of my first blog post. I might come back add some more map making methods later on, but for now the plan is to keep updating things and add more posts. I hope the material here has been informative. Please feel free to contact me about any potential improvements. Thank you free reading my blog, now go make some awesome maps!