Plotting multiple layers
Another typical pandas functionality is filtering a dataframe: taking a subset of the rows based on a condition (which generates a boolean mask).
In this exercise, we will take the subset of all African restaurants, and then make a multi-layered plot. In such a plot, we combine the visualization of several GeoDataFrames on a single figure. To add one layer, we can use the ax
keyword of the plot()
method of a GeoDataFrame to pass it a matplotlib axes object.
The restaurants data is already loaded as the restaurants
GeoDataFrame. GeoPandas is imported as geopandas
and matplotlib.pyplot as plt
.
This is a part of the course
“Working with Geospatial Data in Python”
Exercise instructions
- Select a subset of all rows where the
type
is 'African restaurant'. Call this subsetafrican_restaurants
. - Make a plot of all restaurants and use a uniform grey color. Remember to pass a matplotlib axes object to the
plot()
method. - Add a second layer of only the African restaurants in red. For the typical colors, you can use English names such as 'red' and 'grey'.
- Remove the box using the
set_axis_off()
method on the matplotlib axes object.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Load the restaurants dataset
restaurants = geopandas.read_file("paris_restaurants.geosjon")
# Take a subset of the African restaurants
african_restaurants = ____
# Make a multi-layered plot
fig, ax = plt.subplots(figsize=(10, 10))
restaurants.____
african_restaurants.____
# Remove the box, ticks and labels
ax.____
plt.show()
This exercise is part of the course
Working with Geospatial Data in Python
This course will show you how to integrate spatial data into your Python Data Science workflow.
In this chapter, you will be introduced to the concepts of geospatial data, and more specifically of vector data. You will then learn how to represent such data in Python using the GeoPandas library, and the basics to read, explore and visualize such data. And you will exercise all this with some datasets about the city of Paris.
Exercise 1: Geospatial dataExercise 2: Restaurants in ParisExercise 3: Adding a background mapExercise 4: Introduction to GeoPandasExercise 5: Explore the Paris districts (I)Exercise 6: Explore the Paris districts (II)Exercise 7: The Paris restaurants as a GeoDataFrameExercise 8: Exploring and visualizing spatial dataExercise 9: Visualizing the population densityExercise 10: Using pandas functionality: groupbyExercise 11: Plotting multiple layersWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.