Created
July 28, 2020 15:01
-
-
Save chrisvoncsefalvay/8be2c3fb4005afe9c1fbbfb811f163a8 to your computer and use it in GitHub Desktop.
MWE for Dash frequency bar chart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from random import choices | |
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.DataFrame(choices([1, 2, 3, 4, 5], k=100) for i in range(0, 6)).T | |
df.columns = ["E", "T", "A", "O", "I", "N"] | |
app = dash.Dash() | |
def create_fig(data, selected_column): | |
filtered_df = pd.crosstab(data[selected_column | |
], columns="count").reset_index() | |
fig = go.Figure() | |
traces = go.Bar( | |
x = filtered_df[selected_column].values, | |
y = filtered_df["count"] | |
) | |
fig.add_trace(traces) | |
return fig | |
app.layout = html.Div([ | |
dcc.Graph(id = "freq_graph", figure=create_fig(df, list(df.columns)[0])), | |
dcc.Dropdown(id = "column_pick", | |
options=[{"label": str(i), "value": i} for i in list(df.columns)], | |
value=list(df.columns)[0]) | |
]) | |
@app.callback( | |
Output("freq_graph", "figure"), | |
[Input("column_pick", 'value')]) | |
def update_fig(selected_column): | |
return create_fig(df, selected_column) | |
if __name__ == '__main__': | |
app.run_server(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment