Search code examples
pythonplotlyshapelychoropleth

TypeError: 'MultiPolygon' object is not iterable with Plotly Choropleth


I am trying to make a choropleth map from county level FIPS data using this example from plotly.

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()

When I run this code as is I get TypeError: 'MultiPolygon' object is not iterable.

I see in this post from a year ago that recommends either downgrading Shapely, which I could not get to work still, or to "Access the MultiPolygon's 'geoms' property". I'm new to working with plotly and geo data and find this answer unintuitive. I am wondering if someone could suggest how to edit the code to make this work or is there some other way to solve this issue?


Solution

  • The create_choropleth function has been deprecated for quite a while and the last update of plotly-geo that is used for it was 5 years ago, so I would look for another solution.

    Plotting choropleth maps is now available in plotly.express. An elaborate list of examples can be found here.