我在尝试使用matplotlib的PatchCollections将多边形对象映射到世界地图时遇到了麻烦(我还试图根据我的统计/值为每个多边形着色,但错误在此之前发生,所以我认为它不相关)。
我导入了matplotlib,PatchCollection等等:
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
我的数据位于GeoPandas DataFrame中,名为" poly_df"。
(请注意,我将Multipolygons的每个实例分开,因此每个多边形都有自己的行。例如,挪威在此数据框中有多行,而不是几何列中有多边形的一行,但我的问题甚至在之前就出现了此。)
poly_df:
countryname, value, geometry
Norway, 10.031, POLYGON ((-9.0430908203125 70.80386352539062, ...
当我尝试调用PatchCollection时,在完成绘制海岸线之类的事情之后,像这样:
patches = poly_df.geometry.tolist()
collection = PatchCollection(patches, zorder=5)
这个"补丁中的每个元素"列表是:
<class 'shapely.geometry.polygon.Polygon'>
我收到此错误(geo_plot()是我完成所有这一切的功能):
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'
我是否应该将一些东西传递给PatchCollection(),而不是我目前正在尝试和失败的Polygon对象列表?如果Polygon对象没有这个属性 - 那会是什么?我真的可以使用一些指导,我是地理数据的新手。谢谢。