10  Static Maps

10.1 Static maps

In the HSMA course, we have generally used the geopandas plot method - which is using matplotlib behind the scenes - to produce static maps.

This means that maps are simple to display as we can just use the st.pyplot() function that we used for standard matplotlib charts.

1import geopandas
2import matplotlib.pyplot as plt
import pandas as pd
import streamlit as st

st.title("Crime in Devon by Type")

#########################################################
# Read the geopandas file and create a matplotlib figure
#########################################################

lsoa_2011_crime_figures_df = geopandas.read_file(
3  "https://files.catbox.moe/4o0go1.gpkg"
  )

4fig, ax = plt.subplots()

5lsoa_2011_crime_figures_df.plot(
6  column="sw_5forces_street_by_lsoa_Other crime",
7  legend=True,
8  ax=ax
  )

9st.pyplot(fig)
1
We will need to import the geopandas library to load in and plot a geographic dataset.
2
We also will need to import the matplotlib.pyplot module, which we give the standard alias plt. When working with geopandas in a .py script or a jupyter notebook, this is only necessary when further modifying the plot; however, we need to structure our plot in a slightly different way to get it to display in a streamlit fileapp.
3
We then import a geopackage file from a URL and save it to a variable. As it’s a geopackage rather than a csv or similar, we don’t need to define the CRS: it’s already specified as part of the geopackage file. The same would be true of a geojson.
4
We use the plt.subplots() function, unpacking it to the objects fig and ax by using the syntax fig, ax on the left hand side of our = sign. By leaving this blank, we create a single axis - but the value of this step is it gives us access to these objects so we can pass it to the streamlit function for displaying this sort of plot.
5
We then use the plot method on the dataframe. We don’t need to save the output of this to a variable,
6
We pass in a column to colour the plot by, though this is optional; if we did not, it would just plot the points or polygons in the dataset all in the same colour.
7
We also specify that we want a legend in our plot to give some indication of the range and significance of the clours in the plot.
8
We tell geopandas/matplotlib to plot on the axis we just created using plt.subplots, which we do by passing in ax=ax to our .plot method.
9
The output of using the .plot() method on the geopandas dataframe is a matplotlib.pyplot object. Therefore, we need to use st.pyplot() to display it.
Tip

Take a look at the HSMA geographic modelling and visualisation book to find out more about creating and modifying static maps in python with geopandas and matplotlib.

Note that you will need to adapt the code in those examples slightly to use the same layout as the code above: the main difference is using fig, ax = plt.subplots() and the argument ax=ax in our plot function, but most other aspects will be unchanged.