Telling Better Data Stories with GeoPandas

Joe Ramirez
3 min readOct 7, 2020

--

As I near the end of my data science immersive bootcamp, I am required to create a capstone project that utilizes the techniques and models learned throughout the course. I have yet to finalize what my capstone project idea will be, but I have an inclination that I will need the GeoPandas library to help me better visualize my data. My cohort has not learned GeoPandas as part our curriculum, so I figured it would be the perfect subject for a blog post.

To start, we use pip install in our Jupyter Notebook to install the GeoPandas library as well as a couple other libraries that will help us better visualize our data:

!pip install geopandas
!pip install descartes
!pip install contextily

Once that is done we import our libraries as such:

import geopandas as gpd
import contextily as ctx

To begin using GeoPandas, we need something called a shapefile. I was interested in visualizing a map of Mexico, so I found an appropriate dataset from The Humanitarian Data Exchange.

The file you will download will be a zip file that you will have to unzip. When reading the shapefile contained within that folder, you absolutely must keep the shapefile with all the other files in the folder. Doing otherwise or even renaming any of the files will give you an error and you will be unable to use the shapefile with GeoPandas.

mexico = gpd.read_file('/Users/joseramirez/Documents/Flatiron/blog/mex_admbnda_govmex_20200618_shp/mex_admbnda_adm1_govmex_20200618.shp')

The folder I downloaded had four different shapefiles, but I ended up with this one because it was organized by states.

We can then use mexico.plot() to get a quick visualization of the country:

mexico.plot(figsize=(12,12))

What if we want the states to be in different colors?

mexico.plot(figsize = (12,12), column = 'State', cmap = 'seismic')

As you can see, by specifying column = ‘State’, I was able to tell GeoPandas to color the states themselves differently.

Next, let’s use contextily to take our visualizations to the next level:

ax = mexico.plot(figsize=(12,12), alpha=0.3, edgecolor=’k’)
ctx.add_basemap(ax, zoom=5)

In the above code snippet, the alpha argument modifies the opacity of the map over the background basemap. The default background map is a terrain map but you can also modify this and get different map types:

ax = mexico.plot(figsize=(12,12), alpha=0.3, edgecolor=’k’)
ctx.add_basemap(ax, source = ‘http://b.tile.openstreetmap.org/{z}/{x}/{y}.png', zoom = 5)

The above map is considered a street map and it contains many more details about states and cities than the previous map, but it no longer displays any detail about terrain.

There are many different basemap types you can use for your GeoPandas visualizations. If you would like more information about contextily and basemap types, I recommend this excellent blog.

Conclusion

Although this was a brief introduction to GeoPandas, I hope it helped you to better understand this very powerful library. Your ability to tell exciting data stories will increase exponentially using the GeoPandas library.

--

--

Joe Ramirez
Joe Ramirez

Written by Joe Ramirez

Data Scientist | Data Analyst

No responses yet