Adding a background map
A plot with just some points can be hard to interpret without any spatial context. Therefore, in this exercise we will learn how to add a background map.
We are going to make use of the contextily package. The add_basemap() function of this package makes it easy to add a background web map to our plot. We begin by plotting our data first, and then pass the matplotlib axes object to the add_basemap() function. contextily will then download the web tiles needed for the geographical extent of your plot.
To set the size of the plotted points, we can use the markersize keyword of the plot() method.
Pandas has been imported as pd and matplotlib's pyplot functionality as plt.
Deze oefening maakt deel uit van de cursus
Working with Geospatial Data in Python
Oefeninstructies
- Import
contextily. - Re-do the figure of the previous exercise: make a plot of all the points in
restaurant. - Set the marker size equal to 1 to reduce the size of the points.
- Use the
add_basemap()function ofcontextilyto add a background map: the first argument is the matplotlib axes objectax.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# Read the restaurants csv file
restaurants = pd.read_csv("paris_restaurants.csv")
# Import contextily
____
# A figure of all restaurants with background
fig, ax = plt.subplots()
ax.plot(____, ____, 'o', ____)
contextily.____(____)
plt.show()