Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot the Geometry of one row of a GeoDataFrame

I would like to plot the geometry contained in a single row of a geopandas dataframe, but I am having problems. Here an example

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')

Now if I plot using the command df.plot() I get

enter image description here

but if I try to plot only one row, df.loc[:,0].plot()

I get the following error

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

while if if i try

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

I get AttributeError: 'Polygon' object has no attribute 'plot'

What is the correct way of doing this ?

like image 964
Duccio Piovani Avatar asked Mar 16 '18 14:03

Duccio Piovani


People also ask

Can a GeoDataFrame have two geometry columns?

A GeoDataFrame may also contain other columns with geometrical (shapely) objects, but only one column can be the active geometry at a time.

What is a GeoDataFrame?

A GeoDataFrame object is a pandas. DataFrame that has a column with geometry. In addition to the standard DataFrame constructor arguments, GeoDataFrame also accepts the following keyword arguments: Parameters. crsvalue (optional)


1 Answers

Try it this way:

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

Explanation: shapely.geometry.polygon.Polygon doesn't have .plot() method:

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

geopandas.geoseries.GeoSeries does have .plot() method:

In [20]: type(df.loc[[0],'geometry'])
Out[20]: geopandas.geoseries.GeoSeries
like image 190
MaxU - stop WAR against UA Avatar answered Nov 03 '22 11:11

MaxU - stop WAR against UA