The Paris restaurants as a GeoDataFrame
In the first coding exercise of this chapter, we imported the locations of the restaurants in Paris from a csv file.
To enable the geospatial functionality of GeoPandas, we want to convert the pandas DataFrame to a GeoDataFrame. This can be done with the GeoDataFrame()
constructor and the geopandas.points_from_xy()
function, and is done for you.
Now we have a GeoDataFrame, all spatial functionality becomes available, such as plotting the geometries. In this exercise we will make the same figure as in the first exercise with the restaurants dataset, but now using the GeoDataFrame's plot()
method.
Pandas has been imported as pd
, GeoPandas as geopandas
and matplotlib's pyplot functionality as plt
.
This exercise is part of the course
Working with Geospatial Data in Python
Exercise instructions
- Inspect the first rows of the
restaurants
GeoDataFrame. - Plot it with the
plot()
method of the GeoDataFrame. The return value is a matplotlib axes object: call thisax
. - Again, make sure to set the marker size equal to 1.
- Add a basemap layer using
contextily
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Read the restaurants csv file into a DataFrame
df = pd.read_csv("paris_restaurants.csv")
# Convert it to a GeoDataFrame
restaurants = geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df.x, df.y))
# Inspect the first rows of the restaurants GeoDataFrame
print(restaurants.____)
# Make a plot of the restaurants
ax = restaurants.____
import contextily
contextily.____(____)
plt.show()