Visualizing the population density
Let's get back to the districts dataset. In a previous exercise we visualized the districts with a uniform column. But often we want to show the spatial variation of a variable, and color the polygons accordingly.
In this exercise we will visualize the spatial variation of the population density within the center of Paris. For this, we will first calculate the population density by dividing the population number with the area, and add it as a new column to the dataframe.
The districts dataset is already loaded as districts
, GeoPandas has been imported as geopandas
and matplotlib.pyplot as plt
.
This is a part of the course
“Working with Geospatial Data in Python”
Exercise instructions
- Print the first rows of the districts dataset. Do you see the
'population'
column? - Inspect the area of the districts's geometries.
- Add a column
'population_density'
representing the number of inhabitants per squared kilometer (Note: The area is given in squared meter, so you will need to multiply the result with10**6
). - Plot the districts using the
'population_density'
to color the polygons.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Inspect the first rows of the districts dataset
print(districts.head())
# Inspect the area of the districts
print(districts.____)
# Add a population density column
districts['population_density'] = ____ / ____ * ____
# Make a plot of the districts colored by the population density
districts.____(____, legend=True)
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.