Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -267,6 +267,41 @@ def create_crash_trend_chart(df, weather=None):
|
|
| 267 |
|
| 268 |
return fig
|
| 269 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
def main():
|
| 271 |
st.title('Traffic Crash Analysis')
|
| 272 |
|
|
|
|
| 267 |
|
| 268 |
return fig
|
| 269 |
|
| 270 |
+
def create_category_distribution_chart(df, selected_category, selected_year):
|
| 271 |
+
# Filter by selected year
|
| 272 |
+
if selected_year != 'All Years':
|
| 273 |
+
df = df[df['Year'] == int(selected_year)]
|
| 274 |
+
|
| 275 |
+
# Group by selected category and Injury Severity
|
| 276 |
+
grouped_data = df.groupby([selected_category, 'Injuryseverity']).size().reset_index(name='Count')
|
| 277 |
+
|
| 278 |
+
# Calculate percentages for each category value
|
| 279 |
+
total_counts = grouped_data.groupby(selected_category)['Count'].transform('sum')
|
| 280 |
+
grouped_data['Percentage'] = (grouped_data['Count'] / total_counts * 100).round(2)
|
| 281 |
+
|
| 282 |
+
# Create the stacked bar chart using Plotly
|
| 283 |
+
fig = px.bar(
|
| 284 |
+
grouped_data,
|
| 285 |
+
x=selected_category,
|
| 286 |
+
y='Count',
|
| 287 |
+
color='Injuryseverity',
|
| 288 |
+
text='Percentage',
|
| 289 |
+
title=f'Distribution of Incidents by {selected_category} ({selected_year})',
|
| 290 |
+
labels={'Count': 'Number of Incidents', selected_category: 'Category'},
|
| 291 |
+
height=600,
|
| 292 |
+
)
|
| 293 |
+
|
| 294 |
+
# Customize the chart appearance
|
| 295 |
+
fig.update_traces(texttemplate='%{text}%', textposition='inside')
|
| 296 |
+
fig.update_layout(
|
| 297 |
+
barmode='stack',
|
| 298 |
+
xaxis_tickangle=-45,
|
| 299 |
+
legend_title='Injury Severity',
|
| 300 |
+
margin=dict(t=50, b=100),
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
+
return fig
|
| 304 |
+
|
| 305 |
def main():
|
| 306 |
st.title('Traffic Crash Analysis')
|
| 307 |
|