Home > Software design >  GeoPandas | Plot separate groups of many MultiPolygons
GeoPandas | Plot separate groups of many MultiPolygons

Time:11-07

Goal: Plot 8 graphs; each with 3 legends, and 1-to-many Polygons.

I have 3 lists: extracted_poly, original_poly_flip_y, wkt_poly_flip_y

These represent sources of the same annotations I want to visualise - to find potential discrepancies.

A list contains 8 1-to-many Polygons.

e.g. wkt_poly_flip_y:

[[<shapely.geometry.polygon.Polygon at 0x7f43a335f400>],
 [<shapely.geometry.polygon.Polygon at 0x7f4338a818b0>,
  <shapely.geometry.polygon.Polygon at 0x7f4338a81820>],
 [<shapely.geometry.polygon.Polygon at 0x7f43389c0f70>,
  <shapely.geometry.polygon.Polygon at 0x7f43389c0f10>],
 [<shapely.geometry.polygon.Polygon at 0x7f4338a79d60>,
  <shapely.geometry.polygon.Polygon at 0x7f4338a79d90>,
  <shapely.geometry.polygon.Polygon at 0x7f4338a79c70>],
 [<shapely.geometry.polygon.Polygon at 0x7f4338a79f10>],
 [<shapely.geometry.polygon.Polygon at 0x7f43388c50d0>,
  <shapely.geometry.polygon.Polygon at 0x7f43388c5070>],
 [<shapely.geometry.polygon.Polygon at 0x7f43388c5520>,
  <shapely.geometry.polygon.Polygon at 0x7f43388c5550>,
  <shapely.geometry.polygon.Polygon at 0x7f43388c55e0>],
 [<shapely.geometry.polygon.Polygon at 0x7f43388c53d0>]]

from shapely.geometry import Polygon
import geopandas as gpd

gdf = gpd.GeoDataFrame(['Extracted Tiles', 'Original Tiles', '.wkt'],
                       geometry=[extracted_poly, original_poly_flip_y, wkt_poly_flip_y]
                      ).plot("geometry", cmap="Blues")

Traceback

TypeError: Input must be valid geometry objects: [[<shapely.geometry.polygon.Polygon object at 0x7f4338b678b0>, ...

Converting each list to a flat list of 8 MultiPolygons also errors:

extracted_multipoly = [MultiPolygon(polys) for polys in extracted_poly]

Traceback

TypeError: Input must be valid geometry objects: [<shapely.geometry.multipolygon.MultiPolygon object at 0x7f4336736580>,

Admittedly, I might be getting geopandas and matplotlib .plot() functions muddled up.


Update

How can I plot all 3 on the graph?

I've plotted a list's first MultiPolygon, without a geopandas.GeoDataFrame:

def plot_multipolygon(multipolygon: list):
    slide_annotations = gpd.GeoSeries(multipolygon)
    slide_annotations.plot(color='red', alpha=0.5)
    
    plt.xlim(0)
    plt.ylim(0)
    plt.show()

plot_multipolygon(extracted_multipoly[0]): enter image description here

plot_multipolygon(original_multipoly[0]): enter image description here

plot_multipolygon(wkt_multipoly[0]): enter image description here

CodePudding user response:

for slide in list(range(NUM_SLIDES)):
    d = {'col1': ['Extracted Tiles', 'Original Tiles', '.wkt'], 'geometry': [extracted_multipoly[slide], original_multipoly[slide], wkt_multipoly[slide]]}
    gdf = gpd.GeoDataFrame(d, crs='EPSG:4326').plot(legend=True, alpha=0.5, color=list(mcolors.BASE_COLORS.values()), aspect=1);

First Plot:

enter image description here

  • Related