I am trying to create a bar graph that updates from a multi-select drop-down menu on Dash but when I select an area code to graph, nothing changes.
My dataframe consists of about 11 rows with 4 columns:
Zip Code Area Name percent no internet Mean Income Past 12 Months
0 38126 South Forum 0.8308 26346
1 38106 South Memphis 0.8223 31403
2 38108 Hollywood 0.7356 31278
Here is the code for the callback:
@app.callback(
dash.dependencies.Output('graph1', 'figure'),
[dash.dependencies.Input('demo-dropdown', 'value')])
def update_image_src(selector):
data = []
filtered_data = df[df['Zip Code'] == zip_code]
for zip_code in selector:
data.append(dict(
x=filtered_data['Area Name'][0],
y=filtered_data['percent no internet'][0],
type='bar',
name=zip_code))
figure = {
'data': data,
'layout': {
'title': 'Percent of homes lacking broadband internet',
'xaxis' : dict(
title=data[0],
titlefont=dict(
family='Courier New, monospace',
size=20,
color='#7f7f7f'
)),
'yaxis' : dict(
title='% No Internet',
titlefont=dict(
family='Helvetica, monospace',
size=20,
color='#7f7f7f'
))
}
}
return figure
filtered_data = df[df['Zip Code'] == zip_code]be in the loopfor zip_code in selectorand not before? - Ben.Tfiltered_data, indeed, doing thisfiltered_data['Area Name'][0]means you get the column 'Area Name' and the index with the label 0 not the position 0, so without the being able to see the data I'm not sure, but I guess your index are different labels? - Ben.T