Visualization#

Using matplotlib#

Up to this point we have been plotting our maps using the matplotlib package, which is built into geopandas. While matplotlib is both easy to use and already known to many python users (as it is popular for graphing in general), it has the drawback that it is not interactive.

Using folium#

folium is a python package built to allow the use of leaflet, an open-source JavaScript library. leaflet is an interactive map development toolkit that powers a lot of web-based interactive maps. Alternatives to leaflet include MapBox, Google Maps, as well as many other paid services.

Also built upon leaflet is ipyleaflet, which is a package designed specifically for rendering leaflet maps in Jupyter Notebook. While leaflet is highly interactive and easily builds beautiful maps with little configuration, it depends on JavaScript in order to function.

Let’s replot the above map in folium. Note that folium expects geospatial data to be in EPSG:4326.

import geopandas as gpd
import folium

# file downloaded from https://data.ontario.ca/dataset/ontario-s-health-region-geographic-data
ontario = gpd.read_file(r"../../data/ontario_health_regions/Ontario_Health_Regions.shp")
ontario = ontario[(ontario.REGION != "North")]
ontario = ontario.to_crs(epsg=4326)

# Set starting location, initial zoom, and base layer source.
m = folium.Map(location=[43.67621,-79.40530],zoom_start=6, tiles='cartodbpositron')

for index, row in ontario.iterrows():
    # Simplify each region's polygon as intricate details are unnecessary
    sim_geo = gpd.GeoSeries(row['geometry']).simplify(tolerance=0.001)
    geo_j = sim_geo.to_json()
    geo_j = folium.GeoJson(data=geo_j, name=row['REGION'],style_function=lambda x: {'fillColor': 'black'})
    folium.Popup(row['REGION']).add_to(geo_j)
    geo_j.add_to(m)

m
Make this Notebook Trusted to load map: File -> Trust Notebook