Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +27 -21
src/streamlit_app.py
CHANGED
|
@@ -270,34 +270,40 @@ fig.update_layout(
|
|
| 270 |
# 4. Render
|
| 271 |
st.plotly_chart(fig, use_container_width=True)
|
| 272 |
|
| 273 |
-
#
|
| 274 |
-
|
|
|
|
| 275 |
df
|
| 276 |
-
.groupby(["
|
| 277 |
.size()
|
| 278 |
.reset_index(name="Count")
|
| 279 |
)
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
fig =
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
| 297 |
)
|
| 298 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 299 |
|
|
|
|
|
|
|
|
|
|
| 300 |
|
|
|
|
| 301 |
|
| 302 |
# -------------------------------- Plot 7: Bubble Map of Incident Counts by Region NO MAP --------------------------------
|
| 303 |
# st.markdown("<div class='sectionheader'>Crime Hotspots by Region NO MAP</div>", unsafe_allow_html=True)
|
|
|
|
| 270 |
# 4. Render
|
| 271 |
st.plotly_chart(fig, use_container_width=True)
|
| 272 |
|
| 273 |
+
# -------------------------------- Plot 7: Line Chart for Incident Counts by Region --------------------------------
|
| 274 |
+
# 1. Aggregate total incidents by year
|
| 275 |
+
yearly_region = (
|
| 276 |
df
|
| 277 |
+
.groupby(["year", "RegionName"])
|
| 278 |
.size()
|
| 279 |
.reset_index(name="Count")
|
| 280 |
)
|
| 281 |
+
|
| 282 |
+
# 2. Let the user pick one region to highlight
|
| 283 |
+
regions = sorted(yearly_region["RegionName"].unique())
|
| 284 |
+
sel_region = st.selectbox("Select Region", ["All"] + regions, index=0)
|
| 285 |
+
|
| 286 |
+
if sel_region != "All":
|
| 287 |
+
yearly_region = yearly_region[yearly_region["RegionName"] == sel_region]
|
| 288 |
+
|
| 289 |
+
# 3. Plot a smooth line per region (or just the one selected)
|
| 290 |
+
fig = px.line(
|
| 291 |
+
yearly_region,
|
| 292 |
+
x="year",
|
| 293 |
+
y="Count",
|
| 294 |
+
color="RegionName",
|
| 295 |
+
title=(
|
| 296 |
+
f"Incident Trends Over Time" +
|
| 297 |
+
(f" – {sel_region}" if sel_region!="All" else "")
|
| 298 |
+
),
|
| 299 |
+
labels={"year":"Year", "Count":"Incident Count"}
|
| 300 |
)
|
|
|
|
| 301 |
|
| 302 |
+
# 4. Add LOWESS smoothing (optional)
|
| 303 |
+
for trace in fig.data:
|
| 304 |
+
trace.update(mode="lines") # remove markers
|
| 305 |
|
| 306 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 307 |
|
| 308 |
# -------------------------------- Plot 7: Bubble Map of Incident Counts by Region NO MAP --------------------------------
|
| 309 |
# st.markdown("<div class='sectionheader'>Crime Hotspots by Region NO MAP</div>", unsafe_allow_html=True)
|