Created
September 29, 2022 21:31
-
-
Save AnnMarieW/0de78250a4124bb765eef076e5265c20 to your computer and use it in GitHub Desktop.
Histogram Simulation
This file contains 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
from dash import Dash, html, dcc, Input, Output | |
import numpy as np | |
import plotly.express as px | |
import dash_bootstrap_components as dbc | |
app = Dash(__name__, external_stylesheets=[dbc.themes.SPACELAB]) | |
heading = html.H4( | |
"Normal Distribution Simulation", className="bg-primary text-white p-4" | |
) | |
sample_size = html.Div( | |
[ | |
dbc.Label("Sample Size", html_for="size"), | |
dcc.Slider(1, 1000, value=250, id="size"), | |
], | |
className="mt-2", | |
) | |
n_bins = html.Div( | |
[ | |
dbc.Label("Number of Bins", html_for="n_bins"), | |
dcc.Slider(1, 100, 10, value=20, id="n_bins"), | |
], | |
className="mt-2", | |
) | |
mean = html.Div( | |
[dbc.Label("Mean", html_for="mean"), dbc.Input(id="mean", type="number", value=0)], | |
className="mt-2", | |
) | |
std_dev = html.Div( | |
[ | |
dbc.Label("Standard Deviation", html_for="std_dev"), | |
dbc.Input( | |
id="std_dev", | |
type="number", | |
value=1, | |
), | |
], | |
className="mt-2", | |
) | |
control_panel = dbc.Card( | |
dbc.CardBody( | |
[sample_size, n_bins, mean, std_dev], | |
className="bg-light", | |
) | |
) | |
graph = dbc.Card( | |
[html.Div(id="error_msg", className="text-danger"), dcc.Graph(id="graph")] | |
) | |
app.layout = dbc.Container( | |
[heading, dbc.Row([dbc.Col(control_panel, md=4), dbc.Col(graph, md=6)])], fluid=True | |
) | |
@app.callback( | |
Output("graph", "figure"), | |
Output("error_msg", "children"), | |
Input("mean", "value"), | |
Input("std_dev", "value"), | |
Input("n_bins", "value"), | |
Input("size", "value"), | |
) | |
def callback(m, std_dev, n_bins, n): | |
if m is None or std_dev is None: | |
return {}, "Please enter Standard Deviation and Mean" | |
if std_dev < 0: | |
return {}, "Please enter Standard Deviation > 0" | |
data = np.random.normal(m, std_dev, n) | |
return px.histogram(data, nbins=n_bins), None | |
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