vgosavi2 commited on
Commit
a2ca8f9
·
verified ·
1 Parent(s): abcb3dd

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Prepare source/target/value lists
274
- tm = (
 
275
  df
276
- .groupby(["RegionName","crm_cd_desc"])
277
  .size()
278
  .reset_index(name="Count")
279
  )
280
- grouped = tm.copy()
281
- regions = list(grouped["RegionName"].unique())
282
- crimes = list(grouped["crm_cd_desc"].unique())
283
-
284
- labels = regions + crimes
285
- source = grouped["RegionName"].apply(lambda x: labels.index(x))
286
- target = grouped["crm_cd_desc"].apply(lambda x: labels.index(x))
287
- value = grouped["Count"]
288
-
289
- fig = go.Figure(data=[go.Sankey(
290
- node=dict(label=labels, pad=15, thickness=20),
291
- link=dict(source=source, target=target, value=value)
292
- )])
293
- fig.update_layout(
294
- title_text="Sankey: Region → Crime Type Flows",
295
- font_size=10,
296
- margin=dict(t=50, l=25, r=25, b=25)
 
 
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)