绘制GeoDataFrame的一行的几何图形

时间:2018-03-16 14:43:27

标签: python pandas geopandas

我想绘制一个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()进行绘图,我会得到

enter image description here

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

我收到以下错误

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

如果我尝试

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

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

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

以这种方式尝试:

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