【问题标题】:Plot the Geometry of one row of a GeoDataFrame绘制 GeoDataFrame 的一行的几何图形
【发布时间】:2018-08-25 16:19:07
【问题描述】:

我想绘制包含在 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() 进行绘图,我会得到 ​​p>

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

我收到以下错误

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

如果我尝试一下

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

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

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

【问题讨论】:

    标签: python pandas geopandas


    【解决方案1】:

    试试这个方法:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      相关资源
      最近更新 更多