'How to connect a Plotly Dash dropdown to a Choropleth map?
I've been able to create Choropleth maps from the individual columns in this DataFrame. Here are the first five rows in my dataframe.
FIPS Geographic Area State 4/1/2010 Census 4/1/2020 Census % Population Change Unemployment Rate
0 22001 Acadia LA 61787 57576 -6.815350 3.6
1 22003 Allen LA 25747 22750 -11.640191 4.0
2 22005 Ascension LA 107215 126500 17.987222 2.9
3 22007 Assumption LA 23416 21039 -10.151179 5.3
4 22009 Avoyelles LA 42071 39693 -5.652350 3.7
5 22011 Beauregard LA 35651 36549 2.518863 2.8
Here's the code I have so far.
"""Block of code I built to create the dataframe. I can create a Choropleth using Plotly without Dash using just the % Population Change or the Unemployment Rate column. I want to use a Dash dropdown to change back and forth between the two"""
df = pd.merge(df4, df5, on="Geographic Area", how='inner')
app = dash.Dash()
app.layout = html.Div([
html.H2('Financial Genome Project'),
dcc.Dropdown(
id='census_type',
options=[{"label": x, "value": x}
for x in df.columns[5:]],
value=df.columns[0],
clearable=False,
style={'width': 300},
placeholder="Select a census data type"
),
html.Hr(),
dcc.Graph(id="choropleth-chart"),
])
@app.callback(
Output("choropleth-chart", "figure"),
[Input("census_type", "value")]
)
def display_choropleth(census_type):
fig = px.choropleth(df, geojson=counties,
locations='FIPS',
color=census_type,
color_continuous_scale="rdylgn_r",
range_color=(0, 8),
scope="usa",
hover_name='Geographic Area',
# labels={'% Change': 'Population % Change'}
)
# fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.update_geos(fitbounds='locations', visible=False)
return fig
app.run_server(debug=True)
display_chart()
The dropdown works fine but it doesn't create a Choropleth map and I keep getting call back errors. See below. 
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
