【问题标题】:'Polygon' object is not iterable- iPython Cookbook'Polygon' 对象不可迭代 - iPython Cookbook
【发布时间】:2020-12-03 10:34:09
【问题描述】:

我正在使用Cartopy学习python中数据的可视化

我有这个用于绘制非洲人口和 GDP 的代码。

def choropleth(ax, attr, cmap_name):
    # We need to normalize the values before we can
    # use the colormap.
    values = [c.attributes[attr] for c in africa]
    norm = Normalize(
        vmin=min(values), vmax=max(values))
    cmap = plt.cm.get_cmap(cmap_name)
    for c in africa:
        v = c.attributes[attr]
        sp = ShapelyFeature(c.geometry, crs,
                            edgecolor='k',
                            facecolor=cmap(norm(v)))
        ax.add_feature(sp)


fig, (ax1, ax2) = plt.subplots(
    1, 2, figsize=(10, 16),
    subplot_kw=dict(projection=crs))
draw_africa(ax1)
choropleth(ax1, 'POP_EST', 'Reds')
ax1.set_title('Population')

draw_africa(ax2)
choropleth(ax2, 'GDP_MD_EST', 'Blues')
ax2.set_title('GDP')

并且预期的输出应该是-

但是我收到了这样的错误 -

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-41-b443c58ecbd5> in <module>
      3     subplot_kw=dict(projection=crs))
      4 draw_africa(ax1)
----> 5 choropleth(ax1, 'POP_EST', 'Reds')
      6 ax1.set_title('Population')
      7 

<ipython-input-40-161126226479> in choropleth(ax, attr, cmap_name)
      8     for c in africa:
      9         v = c.attributes[attr]
---> 10         sp = ShapelyFeature(c.geometry, crs,
     11                             edgecolor='k',
     12                             facecolor=cmap(norm(v)))

~/anaconda3/lib/python3.8/site-packages/cartopy/feature/__init__.py in __init__(self, geometries, crs, **kwargs)
    219         """
    220         super(ShapelyFeature, self).__init__(crs, **kwargs)
--> 221         self._geoms = tuple(geometries)
    222 
    223     def geometries(self):

TypeError: 'Polygon' object is not iterable

我尝试在 github 上搜索此问题,但无济于事。谁能帮我解决这个问题?

这里是site 供参考。

【问题讨论】:

  • 这是stackoverflow.com/q/63758107/13208790遇到的同样的错误,你试过那里的答案吗?
  • 是的,我试过..但它的不同@bystander
  • 您上课的食谱现已过期。今天你可以从geopandas开始做专题图。
  • 好的,但我认为 cartopy 语法有问题。我该如何改进呢? @swatchai
  • 如果您只想完成练习,我使用@swatchai 以前的答案之一发布的解决方案应该可以解决它。但是现在这种工作首选 geopandas。

标签: python jupyter-notebook data-visualization cartopy


【解决方案1】:

问题是代码试图将一个有形的Polygon 传递给一个需要MultiPolygon 的函数。 swatchai 在这里https://stackoverflow.com/a/63812490/13208790 的优雅解决方案是捕获Polygons 并将它们放在一个列表中,以便它们可以被视为MultiPolygons。

这是适合您的情况的代码:

for i, c in enumerate(africa):
    v = c.attributes[attr]
    print(i)
    # swatchai's Polygon catch logic
    if c.geometry.geom_type=='MultiPolygon':
        # this is a list of geometries
        sp = ShapelyFeature(c.geometry, crs,
                        edgecolor='k',
                        facecolor=cmap(norm(v)))
    elif c.geometry.geom_type=='Polygon': 
        # this is a single geometry
        sp = ShapelyFeature([c.geometry], crs,
                                edgecolor='k',
                                facecolor=cmap(norm(v)))   
    else:
        pass  #do not plot the geometry

【讨论】:

  • 错字:swatchai :)
  • 那你需要重新编辑。检查代码中的 cmets @swatchai
  • 谢谢!并为我笨拙的打字样本道歉
猜你喜欢
  • 1970-01-01
  • 2019-11-12
  • 2017-01-05
  • 2015-11-11
  • 2021-06-06
  • 2013-05-27
  • 2013-08-02
  • 2012-06-21
相关资源
最近更新 更多