Get startedGet started for free

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.

This exercise is part of the course

Working with Geospatial Data in Python

View Course

Exercise instructions

  • 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 of contextily to add a background map: the first argument is the matplotlib axes object ax.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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()
Edit and Run Code