开始使用免费开始使用

绘制多图层

pandas 的另一个常见功能是过滤 DataFrame:根据条件选取行的子集(该条件会生成布尔掩码)。

在本练习中,您将筛选出所有非洲餐厅的子集,然后绘制一个多图层的可视化。在这样的图中,我们会在同一张图上组合多个 GeoDataFrame 的可视化。要添加一个图层,可以在 GeoDataFrame 的 plot() 方法中使用 ax 关键字,将一个 matplotlib 的坐标轴对象传入。

餐厅数据已作为 restaurants GeoDataFrame 加载。GeoPandas 已以 geopandas 导入,matplotlib.pyplot 已以 plt 导入。

本练习是课程的一部分

在 Python 中处理地理空间数据

查看课程

练习说明

  • 选择所有 type 为 'African restaurant' 的行作为子集。将其命名为 african_restaurants
  • 绘制所有餐厅,并使用统一的灰色。记得向 plot() 方法传入一个 matplotlib 坐标轴对象。
  • 添加第二个仅包含非洲餐厅的图层,并使用红色。常见颜色可直接使用英文名称,如 'red' 和 'grey'。
  • 使用 matplotlib 坐标轴对象的 set_axis_off() 方法移除边框。

交互式实操练习

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

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