2

I am not well versed in Python (or GeoPandas) and I am currently having a simple issue with plotting. To spot some inconsistencies, I need to individually plot all the geometries in the geodataframe. If you are wondering why I am using enumerate() and itertuples() is because I need to find where the inconsistencies are (but I haven't implemented yet in the code!).

Here's the code. Hopefully is something simple enough:

for i, row in enumerate(faci_shp.itertuples(index=False)):
  row.plot()

But I get this error:

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () 1 choma_shp = faci_shp.iloc[facilities, :].copy() 2 for i, row in enumerate(faci_shp.itertuples(index=False)): ----> 3 row.plot()

AttributeError: 'Pandas' object has no attribute 'plot'

Is it clear?

Thanks

1
  • You mean I should row.geometry.plot()? I did that but it threw this error: AttributeError: 'Polygon' object has no attribute 'plot' – user14903989 Feb 1 at 14:28
5

The GeoDataframe

gdf.plot()

enter image description here

Now, what you want to do is to plot the geometry of each individual rows, not the rows. With your solution, you get the entire row with the geometry and the attributes as Pandas dataframes ( hence the error AttributeError: 'Pandas' object has no attribute 'plot')`

for  row in gdf.itertuples():
    print(row)
Pandas(id=1, geometry=<shapely.geometry.polygon.Polygon object at 0x107380160>)
Pandas(id=2, geometry=<shapely.geometry.polygon.Polygon object at 0x11d87e198>)
Pandas(id=3, geometry=<shapely.geometry.polygon.Polygon object at 0x107380080>)

With only the row geometry:

for row in gdf.itertuples():
    print(row.geometry)
POLYGON ((0.7267114943073861 0.7347155512795274, 0.7263063833454005 0.7264693282083019, 0.7250949518994638 0.7183025207730966, 0.7230888667166361 0.7102937797939431, 0.7203074474938659 0.7025202338244942, 0.7167774808187467 0.6950567463618882, 0.712532962200036 0.687975194870347, 0.7076147686723221 0.681343778561904, 0.7020702651278347 0.6752263616007105, 0.6959528481666412 0.669681858056223, 0.6893214318581983 0.6647636645285091, 0.6822398803666569 0.6605191459097985, 0.6747763929040509 0.6569891792346793, 0.667002846934602 0.6542077600119089, 0.6589941059554486 0.6522016748290813, 0.6508272985202432 0.6509902433831446, 0.6425810754490178 0.6505851324211589, 0.6343348523777924 0.6509902433831446, 0.6261680449425869 0.6522016748290812, 0.6181593039634335 0.6542077600119088, 0.6103857579939848 0.6569891792346791, 0.6029222705313786 0.6605191459097983, 0.5958407190398374 0.6647636645285089, 0.5892093027313944 0.6696818580562229, 0.5830918857702009 0.6752263616007104, 0.5775473822257134 0.6813437785619039, 0.5726291886979994 0.6879751948703468, 0.5683846700792887 0.695056746361888, 0.5648547034041695 0.7025202338244942, 0.5620732841813992 0.710293779793943, 0.5600671989985716 0.7183025207730964, 0.5588557675526349 0.7264693282083019, 0.5584506565906492 0.7347155512795273, 0.5588557675526348 0.7429617743507528, 0.5600671989985715 0.7511285817859581, 0.5620732841813992 0.7591373227651116, 0.5648547034041694 0.7669108687345605, 0.5683846700792886 0.7743743561971665, 0.5726291886979993 0.7814559076887079, 0.5775473822257133 0.7880873239971509, 0.5830918857702008 0.7942047409583444, 0.5892093027313942 0.7997492445028316, 0.5958407190398372 0.8046674380305456, 0.6029222705313784 0.8089119566492562, 0.6103857579939844 0.8124419233243755, 0.6181593039634333 0.8152233425471458, 0.6261680449425866 0.8172294277299734, 0.6343348523777921 0.8184408591759101, 0.6425810754490174 0.8188459701378958, 0.6508272985202428 0.8184408591759103, 0.6589941059554483 0.8172294277299735, 0.6670028469346015 0.8152233425471458, 0.6747763929040504 0.8124419233243756, 0.6822398803666566 0.8089119566492566, 0.6893214318581978 0.804667438030546, 0.6959528481666408 0.799749244502832, 0.7020702651278342 0.7942047409583446, 0.7076147686723216 0.7880873239971511, 0.7125329622000357 0.7814559076887084, 0.7167774808187464 0.774374356197167, 0.7203074474938657 0.7669108687345609, 0.723088866716636 0.7591373227651121, 0.7250949518994636 0.7511285817859589, 0.7263063833454004 0.7429617743507535, 0.7267114943073861 0.734715551279528, 0.7267114943073861 0.7347155512795274))
POLYGON ((0.8574398561225293 0.7758907160551732, 0.8570132693400829 0.7672073426158391, 0.8557376172566549 0.7586075948095404, 0.8536251850991509 0.7501742929087525, 0.8506963167440207 0.7419886542208913, 0.8469792187946064 0.7341295109212465, 0.8425096889361334 0.7266725508560383, 0.8373307711844322 0.7196895886270837, 0.8314923413485362 0.713247873977923, 0.8250506266993756 0.707409444142027, 0.818067664470421 0.7022305263903258, 0.8106107044052128 0.6977609965318528, 0.8027515611055679 0.6940438985824384, 0.7945659224177068 0.6911150302273082, 0.7861326205169188 0.6890025980698042, 0.7775328727106202 0.6877269459863762, 0.768849499271286 0.6873003592039298, 0.7601661258319519 0.6877269459863762, 0.7515663780256533 0.6890025980698042, 0.7431330761248652 0.6911150302273081, 0.7349474374370041 0.6940438985824384, 0.7270882941373593 0.6977609965318526, 0.7196313340721511 0.7022305263903257, 0.7126483718431964 0.7074094441420269, 0.7062066571940357 0.7132478739779228, 0.7003682273581396 0.7196895886270835, 0.6951893096064385 0.7266725508560381, 0.6907197797479654 0.7341295109212463, 0.6870026817985511 0.7419886542208911, 0.6840738134434209 0.7501742929087523, 0.6819613812859169 0.7586075948095403, 0.6806857292024888 0.767207342615839, 0.6802591424200425 0.7758907160551731, 0.6806857292024888 0.7845740894945074, 0.6819613812859169 0.7931738373008059, 0.6840738134434208 0.801607139201594, 0.6870026817985511 0.8097927778894551, 0.6907197797479654 0.8176519211891, 0.6951893096064384 0.8251088812543083, 0.7003682273581395 0.8320918434832628, 0.7062066571940355 0.8385335581324236, 0.7126483718431962 0.8443719879683195, 0.7196313340721509 0.8495509057200207, 0.727088294137359 0.8540204355784937, 0.7349474374370037 0.857737533527908, 0.7431330761248649 0.8606664018830382, 0.7515663780256528 0.8627788340405422, 0.7601661258319515 0.8640544861239703, 0.7688494992712857 0.8644810729064166, 0.7775328727106198 0.8640544861239703, 0.7861326205169183 0.8627788340405422, 0.7945659224177064 0.8606664018830383, 0.8027515611055674 0.8577375335279082, 0.8106107044052123 0.8540204355784939, 0.8180676644704206 0.8495509057200209, 0.8250506266993751 0.8443719879683198, 0.8314923413485358 0.8385335581324239, 0.8373307711844318 0.8320918434832633, 0.842509688936133 0.8251088812543087, 0.8469792187946061 0.8176519211891006, 0.8506963167440205 0.8097927778894558, 0.8536251850991508 0.8016071392015947, 0.8557376172566548 0.7931738373008067, 0.8570132693400828 0.7845740894945081, 0.8574398561225293 0.775890716055174, 0.8574398561225293 0.7758907160551732))
POLYGON ((0.8280058963471173 0.6682822096820413, 0.8276007853851317 0.6600359866108159, 0.826389353939195 0.6518691791756106, 0.8243832687563672 0.6438604381964571, 0.8216018495335971 0.6360868922270082, 0.8180718828584779 0.6286234047644021, 0.8138273642397672 0.6215418532728609, 0.8089091707120533 0.6149104369644179, 0.8033646671675658 0.6087930200032244, 0.7972472502063723 0.603248516458737, 0.7906158338979294 0.5983303229310231, 0.7835342824063881 0.5940858043123125, 0.7760707949437821 0.5905558376371932, 0.7682972489743332 0.5877744184144229, 0.7602885079951798 0.5857683332315953, 0.7521217005599744 0.5845569017856586, 0.7438754774887489 0.5841517908236729, 0.7356292544175236 0.5845569017856586, 0.7274624469823181 0.5857683332315952, 0.7194537060031647 0.5877744184144228, 0.7116801600337159 0.5905558376371931, 0.7042166725711098 0.5940858043123123, 0.6971351210795685 0.5983303229310228, 0.6905037047711255 0.6032485164587369, 0.684386287809932 0.6087930200032243, 0.6788417842654446 0.6149104369644178, 0.6739235907377306 0.6215418532728607, 0.6696790721190199 0.6286234047644019, 0.6661491054439007 0.6360868922270082, 0.6633676862211304 0.643860438196457, 0.6613616010383028 0.6518691791756104, 0.6601501695923661 0.6600359866108159, 0.6597450586303804 0.6682822096820412, 0.6601501695923659 0.6765284327532668, 0.6613616010383027 0.6846952401884721, 0.6633676862211304 0.6927039811676255, 0.6661491054439006 0.7004775271370745, 0.6696790721190198 0.7079410145996805, 0.6739235907377304 0.7150225660912218, 0.6788417842654445 0.7216539823996648, 0.6843862878099319 0.7277713993608583, 0.6905037047711253 0.7333159029053455, 0.6971351210795683 0.7382340964330596, 0.7042166725711095 0.7424786150517702, 0.7116801600337156 0.7460085817268894, 0.7194537060031645 0.7487900009496598, 0.7274624469823178 0.7507960861324874, 0.7356292544175232 0.7520075175784241, 0.7438754774887486 0.7524126285404098, 0.752121700559974 0.7520075175784242, 0.7602885079951794 0.7507960861324875, 0.7682972489743327 0.7487900009496598, 0.7760707949437816 0.7460085817268896, 0.7835342824063878 0.7424786150517706, 0.790615833897929 0.7382340964330599, 0.797247250206372 0.733315902905346, 0.8033646671675654 0.7277713993608586, 0.8089091707120528 0.7216539823996651, 0.8138273642397669 0.7150225660912224, 0.8180718828584775 0.707941014599681, 0.8216018495335968 0.7004775271370749, 0.8243832687563671 0.6927039811676261, 0.8263893539391948 0.6846952401884728, 0.8276007853851316 0.6765284327532675, 0.8280058963471173 0.668282209682042, 0.8280058963471173 0.6682822096820413))[Shapely][3]

Now you can plot each row geometry (Shapely geometry) individually with matplotlib

import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6,6))
for row in gdf.itertuples():
     ax.plot(*row.geometry.exterior.xy)
ax.axis('equal')

enter image description here

And descartes

import descartes
fig, ax = plt.subplots(figsize=(6,6))
for row in gdf.itertuples():
  ax.plot(*row.geometry.exterior.xy)
  ax.add_patch(descartes.PolygonPatch(row.geometry, fc='blue', alpha=0.2))
ax.axis('equal')

enter image description here

1
  • What a great explanation. This makes complete sense. Hopefully this also helps many others when trying to iterate over rows. – user14903989 Feb 2 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.