开始使用免费开始使用

将巴黎餐馆数据表示为 GeoDataFrame

在本章的第一个编程练习中,我们从一个 csv 文件中导入了巴黎餐馆的位置。 为了启用 GeoPandas 的空间功能,我们需要将 pandas 的 DataFrame 转换为 GeoDataFrame。可以使用 GeoDataFrame() 构造函数和 geopandas.points_from_xy() 函数来完成,这里已为您准备好。

现在我们已有一个 GeoDataFrame,所有空间功能都可用,例如绘制几何对象。在本练习中,我们将使用餐馆数据集制作与第一个练习相同的图,但这一次使用 GeoDataFrame 的 plot() 方法。

已将 pandas 导入为 pd,GeoPandas 导入为 geopandas,matplotlib 的 pyplot 功能导入为 plt

本练习是课程的一部分

在 Python 中处理地理空间数据

查看课程

练习说明

  • 查看 restaurants GeoDataFrame 的前几行。
  • 使用 GeoDataFrame 的 plot() 方法进行绘图。其返回值是一个 matplotlib 坐标轴对象:将其命名为 ax
  • 同样,将标记大小设置为 1。
  • 使用 contextily 添加底图图层。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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()
编辑并运行代码