-
Notifications
You must be signed in to change notification settings - Fork 0
/
fisher_weight2.py
157 lines (145 loc) · 6.73 KB
/
fisher_weight2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
import pandas as pd
# Initialize Dash app
app = dash.Dash(__name__)
app.title = "Modern Statistical Analysis Visualization"
# Generate random data with the new variables
modern_data = pd.DataFrame({
"Group": ["White", "Black", "Asian", "Hispanic", "Other"],
"IQ": [101, 98, 96, 95, 100],
"Educational Attainment": [90, 75, 79, 70, 80],
"Crime Statistics": [40, 39, 33, 40, 41],
"Income Disparity": [70, 65, 85, 60, 65],
"Health Outcomes": [80, 55, 75, 65, 70]
})
# App layout
app.layout = html.Div(style={"font-family": "Avenir", "padding": "10px", "max-width": "1000px", "margin": "0 auto", "font-size": "0.95em"}, children=[
# Clickable variable titles for toggling visibility
html.Div(
style={"display": "flex", "justify-content": "center", "gap": "8px", "margin-bottom": "10px"},
children=[
html.Div("IQ", id="toggle-iq", style={"color": "#636EFA", "font-weight": "bold", "cursor": "pointer"}),
html.Div("Educational Attainment", id="toggle-education", style={"color": "#EF553B", "font-weight": "bold", "cursor": "pointer"}),
html.Div("Crime Statistics", id="toggle-crime", style={"color": "#00CC96", "font-weight": "bold", "cursor": "pointer"}),
html.Div("Income Disparity", id="toggle-income", style={"color": "#AB63FA", "font-weight": "bold", "cursor": "pointer"}),
html.Div("Health Outcomes", id="toggle-health", style={"color": "#FFA15A", "font-weight": "bold", "cursor": "pointer"})
]
),
# Bar Chart
dcc.Graph(id="modern-chart", style={"width": "100%", "height": "400px", "border": "none"}),
# Sliders with corresponding value display
html.Div(
style={"display": "flex", "justify-content": "space-around", "align-items": "center", "margin-top": "10px"},
children=[
html.Div([
html.Label("IQ", style={"font-weight": "bold", "font-size": "0.85em", "text-align": "center"}),
dcc.Slider(
id="weight-iq",
min=0,
max=5,
step=0.1,
value=1,
marks={i: str(round(i, 1)) for i in [0, 1, 2, 3, 4, 5]}
),
html.Div(id="weight-iq-value", style={"text-align": "center", "font-size": "0.85em", "margin-top": "-10px"})
], style={"width": "18%"}),
html.Div([
html.Label("Educational Attainment", style={"font-weight": "bold", "font-size": "0.85em", "text-align": "center"}),
dcc.Slider(
id="weight-education",
min=0,
max=5,
step=0.1,
value=1,
marks={i: str(round(i, 1)) for i in [0, 1, 2, 3, 4, 5]}
),
html.Div(id="weight-education-value", style={"text-align": "center", "font-size": "0.85em", "margin-top": "-10px"})
], style={"width": "18%"}),
html.Div([
html.Label("Crime Statistics", style={"font-weight": "bold", "font-size": "0.85em", "text-align": "center"}),
dcc.Slider(
id="weight-crime",
min=0,
max=5,
step=0.1,
value=1,
marks={i: str(round(i, 1)) for i in [0, 1, 2, 3, 4, 5]}
),
html.Div(id="weight-crime-value", style={"text-align": "center", "font-size": "0.85em", "margin-top": "-10px"})
], style={"width": "18%"}),
html.Div([
html.Label("Income Disparity", style={"font-weight": "bold", "font-size": "0.85em", "text-align": "center"}),
dcc.Slider(
id="weight-income",
min=0,
max=5,
step=0.1,
value=1,
marks={i: str(round(i, 1)) for i in [0, 1, 2, 3, 4, 5]}
),
html.Div(id="weight-income-value", style={"text-align": "center", "font-size": "0.85em", "margin-top": "-10px"})
], style={"width": "18%"}),
html.Div([
html.Label("Health Outcomes", style={"font-weight": "bold", "font-size": "0.85em", "text-align": "center"}),
dcc.Slider(
id="weight-health",
min=0,
max=5,
step=0.1,
value=1,
marks={i: str(round(i, 1)) for i in [0, 1, 2, 3, 4, 5]}
),
html.Div(id="weight-health-value", style={"text-align": "center", "font-size": "0.85em", "margin-top": "-10px"})
], style={"width": "18%"})
]
)
])
# Callback to update chart and toggle visibility
@app.callback(
Output("modern-chart", "figure"),
[Input("weight-iq", "value"),
Input("weight-education", "value"),
Input("weight-crime", "value"),
Input("weight-income", "value"),
Input("weight-health", "value"),
Input("toggle-iq", "n_clicks"),
Input("toggle-education", "n_clicks"),
Input("toggle-crime", "n_clicks"),
Input("toggle-income", "n_clicks"),
Input("toggle-health", "n_clicks")]
)
def update_modern_chart(weight_iq, weight_education, weight_crime, weight_income, weight_health, *toggle_clicks):
visibility = [True, True, True, True, True]
# Toggle visibility based on click counts
for i, clicks in enumerate(toggle_clicks):
if clicks and clicks % 2 == 1:
visibility[i] = False
weighted_data = modern_data.copy()
weighted_data["IQ"] *= weight_iq
weighted_data["Educational Attainment"] *= weight_education
weighted_data["Crime Statistics"] *= weight_crime
weighted_data["Income Disparity"] *= weight_income
weighted_data["Health Outcomes"] *= weight_health
fig = go.Figure()
for col, color, is_visible in zip(
["IQ", "Educational Attainment", "Crime Statistics", "Income Disparity", "Health Outcomes"],
["#636EFA", "#EF553B", "#00CC96", "#AB63FA", "#FFA15A"],
visibility
):
if is_visible:
fig.add_trace(go.Bar(x=weighted_data["Group"], y=weighted_data[col], name=col, marker=dict(color=color), opacity=0.75))
fig.update_layout(
barmode="group",
plot_bgcolor="white",
font=dict(family="Avenir"),
xaxis=dict(showgrid=False, showline=True, zeroline=False, title="Group"),
yaxis=dict(showgrid=True, showticklabels=False, title=""),
showlegend=False,
margin=dict(l=10, r=10, t=20, b=10)
)
return fig
if __name__ == "__main__":
app.run_server(debug=True, port=8050)