TypeError: 'MultiPolygon' 对象不可迭代

问题描述 投票:0回答:1

我正在尝试从 plotly 运行以下脚本:https://plotly.com/python/county-choropleth/

我一开始就收到错误代码:TypeError: 'MultiPolygon' object is not iterable

我查过几篇帖子,其中有类似的问题,但我怀疑这些是针对这个特定问题的解决方案。选项 2 似乎是更可能的方法,但为什么会有一种用于 plotly 发布的简单编码的解决方法?似乎我可能在编写代码的方式中遗漏了一些东西。

选项 1:“多边形”对象不可迭代 - iPython Cookbook

选项 2:Python:从 Shapefile 到彩色 cartopy map 的 Dataframe 中的多边形迭代

import plotly.figure_factory as ff

import numpy as np
import pandas as pd

df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv')
df_sample_r = df_sample[df_sample['STNAME'] == 'Florida']

values = df_sample_r['TOT_POP'].tolist()
fips = df_sample_r['FIPS'].tolist()

endpts = list(np.mgrid[min(values):max(values):4j])
colorscale = ["#030512","#1d1d3b","#323268","#3d4b94","#3e6ab0",
              "#4989bc","#60a7c7","#85c5d3","#b7e0e4","#eafcfd"]
fig = ff.create_choropleth(
    fips=fips, values=values, scope=['Florida'], show_state_data=True,
    colorscale=colorscale, binning_endpoints=endpts, round_legend_values=True,
    plot_bgcolor='rgb(229,229,229)',
    paper_bgcolor='rgb(229,229,229)',
    legend_title='Population by County',
    county_outline={'color': 'rgb(255,255,255)', 'width': 0.5},
    exponent_format=True,
)
fig.layout.template = None
fig.show()
python plotly plotly-dash geopandas
1个回答
0
投票

降级到较低版本。例子:

pip3 install shapely==1.7.1

Versions>=2.0 给我错误。

© www.soinside.com 2019 - 2024. All rights reserved.