首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >“‘Polygon”对象不可迭代- iPython Cookbook

“‘Polygon”对象不可迭代- iPython Cookbook
EN

Stack Overflow用户
提问于 2020-12-03 18:34:10
回答 1查看 1.5K关注 0票数 0

我正在学习使用Cartopy在python中实现数据可视化。

我有这个代码来绘制非洲的人口和国内生产总值。

代码语言:javascript
运行
复制
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')

并且预期的输出应该是-

但我得到了一个错误-

代码语言:javascript
运行
复制
---------------------------------------------------------------------------
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-03 20:49:48

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

以下是根据您的情况调整的代码:

代码语言:javascript
运行
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65124253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档