I'm having trouble trying to map polygon objects onto a world map using matplotlib's PatchCollections (I'm also trying to color each polygon according to a statistic/value I have, but the error occurs before this, so I don't think it is relevant).
I imported matplotlib, PatchCollection, and others as so:
import pandas as pd
import matplotlib as mpl
import numpy as np
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
mpl.use('TkAgg')
import matplotlib.pyplot as plt
import geopandas as gp
import shapely
My data is in a GeoPandas DataFrame called "poly_df".
(Note that I separated out each instance of Multipolygons so each polygon has its own row. For example, Norway has multiple rows in this dataframe rather than one row with a multipolygon in the geometry column, but my problem was occuring even before this.)
poly_df:
countryname, value, geometry
Norway, 10.031, POLYGON ((-9.0430908203125 70.80386352539062, ...
When I try to call PatchCollection, after doing things like drawing coastlines etc, like this:
patches = poly_df.geometry.tolist()
collection = PatchCollection(patches, zorder=5)
where each element in this "patches" list is:
<class 'shapely.geometry.polygon.Polygon'>
I'm getting this error (geo_plot() is my function where all this is being done):
Traceback (most recent call last):
File "labor_participation.py", line 125, in <module>
geo_plot()
File "labor_participation.py", line 109, in geo_plot
collection = PatchCollection(patches, zorder=5)
File "/Users/Martin/anaconda/lib/python3.5/site-
packages/matplotlib/collections.py", line 1696, in __init__
self.set_paths(patches)
File "/Users/Martin/anaconda/lib/python3.5/site-
packages/matplotlib/collections.py", line 1700, in set_paths
for p in patches]
File "/Users/Martin/anaconda/lib/python3.5/site-
packages/matplotlib/collections.py", line 1700, in <listcomp>
for p in patches]
AttributeError: 'Polygon' object has no attribute 'get_transform'
Am I supposed to pass something into PatchCollection() other than the list of Polygon objects that I'm currently trying, and failing, to use? If Polygon objects don't have this attribute - what does? I could really use some guidance, I'm new to geographic data. Thank you.