Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Working with Geospatial Data in Python

Cursus bekijken

Oefeninstructies

  • 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 this ax.
  • Again, make sure to set the marker size equal to 1.
  • Add a basemap layer using contextily.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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()
Code bewerken en uitvoeren