0% found this document useful (0 votes)
4 views30 pages

Understanding Choropleths in Python

The document provides an overview of choropleths and their application in visualizing geospatial data using Python libraries such as GeoPandas and Folium. It details the process of calculating school density in various districts and creating visual representations of this data on maps. Additionally, it includes practical examples and skills related to handling geospatial data and creating informative visualizations.

Uploaded by

abenes10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views30 pages

Understanding Choropleths in Python

The document provides an overview of choropleths and their application in visualizing geospatial data using Python libraries such as GeoPandas and Folium. It details the process of calculating school density in various districts and creating visual representations of this data on maps. Additionally, it includes practical examples and skills related to handling geospatial data and creating informative visualizations.

Uploaded by

abenes10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is a

choropleth?
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N

Mary van Valkenburg


Data Science Program Manager,
Nashville So ware School
Definition of a choropleth

VISUALIZING GEOSPATIAL DATA IN PYTHON


VISUALIZING GEOSPATIAL DATA IN PYTHON
Density
schools_in_districts.head(2)

district geometry name lat lng


1 (POLYGON ((-86.77 36.38... Nashville Prep 36.16 -86.85
1 (POLYGON ((-86.77 36.38... Rocketship Prep 36.17 -86.79

VISUALIZING GEOSPATIAL DATA IN PYTHON


Get counts
school_counts = schools_in_districts.groupby(['district']).size()
print(school_counts)

district
1 30
2 11
3 19
4 18
5 36
6 21
7 13
8 10
9 12
dtype: int64

VISUALIZING GEOSPATIAL DATA IN PYTHON


Add counts
school_counts_df = school_counts.to_frame()
school_counts_df = school_counts_df.reset_index()
school_counts_df.columns = ['district', 'school_count']

districts_with_counts = [Link](school_districts, school_counts_df,


on = 'district')
districts_with_counts.head(2)

district geometry school_count


1 (POLYGON ((-86.77 36.38... 30
3 (POLYGON ((-86.75 36.40... 19

VISUALIZING GEOSPATIAL DATA IN PYTHON


Divide counts by areas
districts_with_counts['area'] = districts_with_counts.[Link]

districts_with_counts['school_density'] = districts_with_counts.apply(
lambda row: row.school_count/[Link], axis = 1)

districts_with_counts.head(2)

district geometry school_count area school_density


1 (POLYGON ((-86.77 36.38... 30 0.036641 818.745403
3 (POLYGON ((-86.75 36.40... 19 0.014205 1337.594495

VISUALIZING GEOSPATIAL DATA IN PYTHON


Let's Practice!
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N
Choropleths with
geopandas
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N

Mary van Valkenburg


Data Science Program Manager,
Nashville So ware School
VISUALIZING GEOSPATIAL DATA IN PYTHON
Choropleth with [Link]()
districts_with_counts.plot(column = 'school_density', legend = True)
[Link]('Schools per decimal degrees squared area')
[Link]('longitude')
[Link]('latitude');

VISUALIZING GEOSPATIAL DATA IN PYTHON


Choropleth with [Link]()
districts_with_counts.plot(column = 'school_density', cmap = 'BuGn', edgecolor = 'black', legend =
[Link]('Schools per decimal degrees squared area')
[Link]('longitude')
[Link]('latitude');

VISUALIZING GEOSPATIAL DATA IN PYTHON


Area in Kilometers Squared
# starting CRS
print(school_districts.crs)

{'init': 'epsg:4326'}

# convert to EPSG 3857


school_districts = school_districts.to_crs(epsg = 3857)
print(school_districts.crs)

{'init': 'epsg:3857', 'no_defs': True}

VISUALIZING GEOSPATIAL DATA IN PYTHON


Area in Kilometers Squared
# define a variable for m^2 to km^2
sqm_to_sqkm = 10**6

school_districts['area'] = school_districts.[Link] / sqm_to_sqkm


school_districts.head(2)

district geometry area


1 (POLYGON ((-965.055 4353528.766... 563.134380
3 (POLYGON ((-965.823 4356392.677... 218.369949

VISUALIZING GEOSPATIAL DATA IN PYTHON


# change crs back to 4326
school_districts = school_districts.to_crs(epsg = 4326)
print(school_districts.crs)

{'init': 'epsg:4326', 'no_defs': True}

print(school_districts.head(2))

district geometry area


1 (POLYGON ((-86.771 36.383... 563.134380
3 (POLYGON ((-86.753 36.404... 218.369949

# spatial join to get districts that contain schools


schools_in_districts = [Link](school_districts, schools_geo, op = 'contains')

VISUALIZING GEOSPATIAL DATA IN PYTHON


# aggregate to get counts
school_counts = schools_in_districts.groupby(['district']).size()

# convert school_counts to a df
school_counts_df = school_counts.to_frame()
school_counts_df = school_counts_df.reset_index(level=0)
school_counts_df.columns = ['district', 'school_count']

# merge
districts_with_counts = [Link](school_districts,
school_counts_df, on = 'district')

districts_with_counts.head(1)

district geometry area school_count


1 (POLYGON ((-86.771 36.383.. 563.134380 30

VISUALIZING GEOSPATIAL DATA IN PYTHON


Calculating school density
# create school_density
districts_with_counts['school_density'] = districts_with_counts.apply(
lambda row: row.school_count/[Link], axis = 1)

# plot it
districts_with_counts.plot(column = 'school_density', cmap = 'BuGn',
edgecolor = 'black', legend = True)
[Link]('Schools per kilometers squared')
[Link]('longitude')
[Link]('latitude')
[Link]();

VISUALIZING GEOSPATIAL DATA IN PYTHON


VISUALIZING GEOSPATIAL DATA IN PYTHON
Let's practice!
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N
Choropleths with
folium
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N

Mary van Valkenburg


Data Science Program Manager,
Nashville So ware School
[Link] choropleth
# Construct a map object for Nashville
nashville = [36.1636,-86.7823]
m = [Link](location=nashville, zoom_start=10)

# Create a choropleth
[Link](...)

VISUALIZING GEOSPATIAL DATA IN PYTHON


Arguments of the folium choropleth
geo_data - the source data for the polygons (geojson le or a GeoDataFrame)

name - the name of the geometry column (or geojson property) for the polygons

data - the source DataFrame or Series for the normalized data

columns - a list of columns: one that corresponds to the polygons and one that has the
value to plot

VISUALIZING GEOSPATIAL DATA IN PYTHON


Additional arguments of the folium choropleth
key_on - a GeoJSON variable to bind the data to (always starts with feature )

fill_color - polygon ll color (defaults to blue)

fill_opacity - range between 0 (transparent) and 1 (completely opaque)

line_color - color of polygon border lines (defaults to black)

line_opacity - range between 0 (transparent) and 1 (completely opaque)

legend_name - creates a title for the legend

VISUALIZING GEOSPATIAL DATA IN PYTHON


# Center point and map for Nashville
nashville = [36.1636,-86.7823]
m = [Link](location=nashville, zoom_start=10)

# Define a choropleth layer for the map


[Link](
geo_data=districts_with_counts,
name='geometry',
data=districts_with_counts,
columns=['district', 'school_density'],
key_on='[Link]',
fill_color='YlGn',
fill_opacity=0.75,
line_opacity=0.5,
legend_name='Schools per km squared by School District'
)

# Add layer control and display


[Link]().add_to(m)
display(m)

VISUALIZING GEOSPATIAL DATA IN PYTHON


Folium choropleth of school density

VISUALIZING GEOSPATIAL DATA IN PYTHON


Let's Practice!
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N
Congratulations!
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N

Mary van Valkenburg


Data Science Program Manager,
Nashville So ware School
Skills list
how to work with shape les and GeoJSON

how to work with geometries

how to use geopandas, shapely, and folium to extract meaning from geospatial data

how to create beautiful and informative geospatial visualizations

VISUALIZING GEOSPATIAL DATA IN PYTHON


VISUALIZING GEOSPATIAL DATA IN PYTHON
Goodbye
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N

You might also like