Layered Maps#

These mapping examples uses both the district maps and school location data to create visualizations and interactive maps.

Examples include:

  • calling multiple plots to create map layers

  • changing the mouse over and mouse click data for interaction

# uncomment and run if using Google Colab

# !pip install geopandas
# !pip install nycschools
# from nycschools import dataloader
# dataloader.download_data()
import pandas as pd
import geopandas as gpd
import folium
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.display import Markdown as md

from nycschools import schools, ui, geo

from IPython.display import display, HTML
# load the school demographics and the geo data for schools and districts
demo = schools.load_school_demographics()
df = geo.load_school_locations()
districts = geo.load_districts()

Plotting twice: schools inside of districts#

districts contains the shape files for the 32 geographic districts in the NYC Schools.

In the cell below, we create a new figure and axis and then plot both the districts and the schools using ax – the same axis. This makes a graph where schools are plotted on top of the districts.

# create the plot
fig, ax = plt.subplots(figsize=(8, 8))

# first, plot the districts onto `ax`
districts.plot(ax=ax, color="lightgray", edgecolor="black", linewidth=1)
# second, plot the schools onto `ax`
_ = df.plot(ax=ax,color="red")

Folium map with styles#

We’ve seen how the explore() function generates an interactive, geographic Folium map. In this example we create an interactive district map where we specify more of the styles for the map to give it a unique look. We save the map into the district_map variable because we’re going to re-use it later.

district_map = districts.explore(
     column="district", # use district for the category colors (aka choropleth)
     popup=False, # turn things off
     tiles="CartoDB positron", # use "CartoDB positron" tiles
     cmap="tab20b", # use "tab20b" matplotlib colormap
     style_kwds={"color":"black"} # use black outline

# add a label to each district with the district number
ui.label_shapes(district_map, districts, "district", style={"color":"black", "font-size":"16px"})
Make this Notebook Trusted to load map: File -> Trust Notebook