Plotting GeoJson Files with Matplotlib

Timothy Mango
3 min readJul 16, 2019

--

Visualizing Superfund Site Location at Census Block Group Level

This is an initial visualization for my Flatiron Data Science final project. My final project plan can be found here. The data is described and the objective is defined in the project plan. The placeholder visualization in this blog post will be updated and used in my final project presentation. Right now the visualization doesn’t have meaning.

This is a relatively short post but the full Jupyter notebook for this project will be updated and linked soon. Get ready for a bit of Python!

Import packages

import pandas as pd
import numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
%matplotlib inline

Read GeoJson File to Geopandas

#read in once
#df_places = gpd.read_file('cbg.geojson')

Examine First Index of Geometry.

This is one census block group!

df_places['geometry'][0]

Examine entire Geometry Column

This is just the combination of the geometry for every row of the dataset (the combination of all census block groups).

df_places['geometry']
#same as df_places.plot()
This visualization could use improving…

Create placeholder variable

The placeholder variable is intended to represent the false positive counts that will be generated from my prediction models. Right now the count does not have any meaning. This is just a test to see if this would be a good final visualization method.

#The first line of code creates a new column full of 0 values
#The second line of code replaces the first 1700 rows with a
#random number between 1 and 7
merged_df['Count_Placeholder'] = 0
merged_df['Count_Placeholder'][:1700] = np.random.randint(7, size=1700)

My Matplotlib Cmap (afmhot) goes from dark to light. This is supposed to represent a heatmap for the two following plots.

Plot placeholder variable and resize map

f, ax = plt.subplots(1, figsize=(15, 15))
ax = merged_df.plot(column='Count_Placeholder', cmap='afmhot' ,ax=ax)
ax.set_xlim([-125, -67])
ax.set_ylim([24, 50])
plt.show()

Adjust axis to zoom in on Alabama

I just happened to assign the placeholder variable to census block groups in Alabama (the dataset was in alphabetical order).

f, ax = plt.subplots(1, figsize=(10, 10))
ax = merged_df.plot(column='Count_Placeholder', cmap='afmhot' ,ax=ax)
ax.set_xlim([-91, -81])
ax.set_ylim([27, 37])
plt.show()

Plot census blocks of actual superfund site locations

This is the visualization I was trying to create! Datasets were merged to create this viz. ‘target_2’ is just a column with 0 and 1 values indicating if the census block group contains a superfund site.

f, ax = plt.subplots(1, figsize=(15, 15))
ax = merged_df.plot(column='target_2', cmap='gnuplot', ax=ax)
ax.set_xlim([-125, -67])
ax.set_ylim([24, 50])
plt.show()

Tada!

See you next time…

--

--