绘制 GeoDataFrame 一行的几何图形

Plot the Geometry of one row of a GeoDataFrame

我想绘制 geopandas 数据框的单行中包含的几何图形,但我遇到了问题。举个例子

import geopandas as gpd
import numpy as np
from shapely.geometry import Polygon

p1 = Polygon([(0, 0), (1, 0), (1, 1)])
p2 = Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
p3 = Polygon([(1, 1), (2, 1), (2, 2), (1, 2)])
index = np.random.random(3)
df = gpd.GeoDataFrame()
df['index'] = index
df['geometry'] = [p1,p2,p3]
df = df.set_geometry('geometry')

现在,如果我使用命令 df.plot() 绘图,我会得到

但是如果我尝试只绘制一行,df.loc[:,0].plot()

我收到以下错误

TypeError: Empty 'DataFrame': no numeric data to plot,

而如果我尝试

df.loc[:,'geometry'].plot()

我得到AttributeError: 'Polygon' object has no attribute 'plot'

正确的做法是什么?

这样试试:

df.loc[[0],'geometry'].plot()
#      ^ ^

解释:shapely.geometry.polygon.Polygon 没有.plot()方法:

In [19]: type(df.loc[0,'geometry'])
Out[19]: shapely.geometry.polygon.Polygon

geopandas.geoseries.GeoSeries 是否.plot()方法:

In [20]: type(df.loc[[0],'geometry'])
Out[20]: geopandas.geoseries.GeoSeries