I’m new to Dash and I’ve been trying to create a simple barchart with a dropdown being the column names in a dataframe. The bar chart that it is supposed to create is a frequency table using pd.crosstab, based on the dropdown selected. The error I’m getting is
Invalid argument figure.data
passed into Graph with ID “freq_graph”.
Expected an array.
Was supplied type object
.
Below is my code below. would appreciate any guidance.
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from dash.dependencies import Input, Output
df = pd.read_csv(‘data.csv’)
df_cols = df.columns.to_list()
app = dash.Dash()
all_columns =
for i in df_cols:
all_columns.append({‘label’:i, ‘value’:i})
app.layout = html.Div([
dcc.Graph(id = ‘freq_graph’),
dcc.Dropdown(id = ‘column_pick’,
options = all_columns,
value = ‘STATE’)
])
@app.callback(
Output(‘freq_graph’, ‘figure’),
[Input(‘column_pick’, ‘value’)])
def update_fig(selected_column):
filtered_df = pd.crosstab(df[selected_column], columns=‘count’).reset_index()
traces = go.Bar(
x=filtered_df[selected_column],
y=filtered_df[‘count’]
)
return {
'data': traces,
'layout': go.Layout(xaxis={'groupings'}
)
}
if name == ‘main’:
app.run_server(debug = True)