Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +47 -69
src/streamlit_app.py
CHANGED
|
@@ -269,83 +269,61 @@ fig.update_layout(
|
|
| 269 |
# 4. Render
|
| 270 |
st.plotly_chart(fig, use_container_width=True)
|
| 271 |
|
| 272 |
-
#
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
# 1. Aggregate total incidents by region and pick top 10
|
| 276 |
-
region_counts = (
|
| 277 |
df
|
| 278 |
-
.groupby("RegionName")
|
| 279 |
-
.size() # count rows
|
| 280 |
-
.reset_index(name="Count") # turn it into a DataFrame with columns RegionName & Count
|
| 281 |
-
)
|
| 282 |
-
top_regions = region_counts.head(10)
|
| 283 |
-
st.write(top_regions)
|
| 284 |
-
# 2. Build the bubble chart
|
| 285 |
-
fig = px.scatter(
|
| 286 |
-
top_regions,
|
| 287 |
-
x='Count',
|
| 288 |
-
y='RegionName',
|
| 289 |
-
size='Count', # bubble area ∝ incident count
|
| 290 |
-
color='Count', # color scale also shows volume
|
| 291 |
-
hover_name='RegionName', # show region on hover
|
| 292 |
-
hover_data={'Count':True},
|
| 293 |
-
size_max=60, # max bubble diameter
|
| 294 |
-
title='Top 10 Regions by Crime Volume (Bubble Chart)'
|
| 295 |
-
)
|
| 296 |
-
|
| 297 |
-
# 3. Tweak layout
|
| 298 |
-
fig.update_layout(
|
| 299 |
-
xaxis_tickangle=-45, # tilt x-labels so they’re legible
|
| 300 |
-
margin=dict(t=50, b=100),
|
| 301 |
-
yaxis_title='Incident Count',
|
| 302 |
-
xaxis_title=''
|
| 303 |
-
)
|
| 304 |
-
|
| 305 |
-
# 4. Render in Streamlit
|
| 306 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 307 |
-
##########################################################
|
| 308 |
-
# Aggregate total incidents by region and take top 10
|
| 309 |
-
region_counts = (
|
| 310 |
-
df.groupby("RegionName")
|
| 311 |
.size()
|
| 312 |
.reset_index(name="Count")
|
| 313 |
-
.nlargest(10, "Count")
|
| 314 |
)
|
| 315 |
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
|
|
|
|
|
|
| 321 |
)
|
|
|
|
|
|
|
| 322 |
|
| 323 |
-
# 3. Plot packed bubbles
|
| 324 |
-
fig, ax = plt.subplots(figsize=(3, 3))
|
| 325 |
-
ax.axis("off")
|
| 326 |
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
#
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
#
|
| 348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
|
| 350 |
# -------------------------------- Plot 2: Heat Map --------------------------------
|
| 351 |
st.markdown("<div class='sectionheader'> HeatMap </div>", unsafe_allow_html=True)
|
|
|
|
| 269 |
# 4. Render
|
| 270 |
st.plotly_chart(fig, use_container_width=True)
|
| 271 |
|
| 272 |
+
# Aggregate counts by region & crime
|
| 273 |
+
sun = (
|
|
|
|
|
|
|
|
|
|
| 274 |
df
|
| 275 |
+
.groupby(["RegionName","crm_cd_desc"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
.size()
|
| 277 |
.reset_index(name="Count")
|
|
|
|
| 278 |
)
|
| 279 |
|
| 280 |
+
fig = px.sunburst(
|
| 281 |
+
sun,
|
| 282 |
+
path=["RegionName","crm_cd_desc"],
|
| 283 |
+
values="Count",
|
| 284 |
+
color="Count",
|
| 285 |
+
color_continuous_scale="Agsunset",
|
| 286 |
+
title="Crime Distribution: Region → Crime Type"
|
| 287 |
)
|
| 288 |
+
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
|
| 289 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 290 |
|
|
|
|
|
|
|
|
|
|
| 291 |
|
| 292 |
+
|
| 293 |
+
# -------------------------------- Plot 7: Bubble Map of Incident Counts by Region NO MAP --------------------------------
|
| 294 |
+
# st.markdown("<div class='sectionheader'>Crime Hotspots by Region NO MAP</div>", unsafe_allow_html=True)
|
| 295 |
+
|
| 296 |
+
# # 1. Aggregate total incidents by region and pick top 10
|
| 297 |
+
# region_counts = (
|
| 298 |
+
# df
|
| 299 |
+
# .groupby("RegionName") # group by your text field
|
| 300 |
+
# .size() # count rows
|
| 301 |
+
# .reset_index(name="Count") # turn it into a DataFrame with columns RegionName & Count
|
| 302 |
+
# )
|
| 303 |
+
# top_regions = region_counts.head(10)
|
| 304 |
+
# # 2. Build the bubble chart
|
| 305 |
+
# fig = px.scatter(
|
| 306 |
+
# top_regions,
|
| 307 |
+
# x='Count',
|
| 308 |
+
# y='RegionName',
|
| 309 |
+
# size='Count', # bubble area ∝ incident count
|
| 310 |
+
# color='Count', # color scale also shows volume
|
| 311 |
+
# hover_name='RegionName', # show region on hover
|
| 312 |
+
# hover_data={'Count':True},
|
| 313 |
+
# size_max=60, # max bubble diameter
|
| 314 |
+
# title='Top 10 Regions by Crime Volume (Bubble Chart)'
|
| 315 |
+
# )
|
| 316 |
+
|
| 317 |
+
# # 3. Tweak layout
|
| 318 |
+
# fig.update_layout(
|
| 319 |
+
# xaxis_tickangle=-45, # tilt x-labels so they’re legible
|
| 320 |
+
# margin=dict(t=50, b=100),
|
| 321 |
+
# yaxis_title='Incident Count',
|
| 322 |
+
# xaxis_title=''
|
| 323 |
+
# )
|
| 324 |
+
|
| 325 |
+
# # 4. Render in Streamlit
|
| 326 |
+
# st.plotly_chart(fig, use_container_width=True)
|
| 327 |
|
| 328 |
# -------------------------------- Plot 2: Heat Map --------------------------------
|
| 329 |
st.markdown("<div class='sectionheader'> HeatMap </div>", unsafe_allow_html=True)
|