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

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +39 -0
src/streamlit_app.py CHANGED
@@ -304,6 +304,45 @@ 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)
 
304
  trace.update(mode="lines") # remove markers
305
 
306
  st.plotly_chart(fig, use_container_width=True)
307
+ # -------------------------------- Plot 8: Line Chart for Incident Counts by Region --------------------------------
308
+ # 1. Compute counts per region and crime
309
+ counts = (
310
+ df
311
+ .groupby(['RegionName', 'crm_cd_desc'])
312
+ .size()
313
+ .reset_index(name='Count')
314
+ )
315
+
316
+ # 2. For each region, keep only its top 5 crime types
317
+ top5_per_region = (
318
+ counts
319
+ .groupby('RegionName', group_keys=False)
320
+ .apply(lambda grp: grp.nlargest(5, 'Count'))
321
+ )
322
+
323
+ # 3. Draw a stacked bar chart
324
+ fig = px.bar(
325
+ top5_per_region,
326
+ x='RegionName',
327
+ y='Count',
328
+ color='crm_cd_desc',
329
+ title='Top 5 Crimes by Region',
330
+ labels={'crm_cd_desc': 'Crime Type'},
331
+ height=600
332
+ )
333
+
334
+ # 4. Tweak layout for readability
335
+ fig.update_layout(
336
+ barmode='stack',
337
+ xaxis_tickangle=-45,
338
+ xaxis_title='',
339
+ yaxis_title='Incident Count',
340
+ legend_title_text='Crime Type',
341
+ margin=dict(t=50, b=150, l=50, r=50)
342
+ )
343
+
344
+ # 5. Render in Streamlit
345
+ st.plotly_chart(fig, use_container_width=True)
346
 
347
  # -------------------------------- Plot 7: Bubble Map of Incident Counts by Region NO MAP --------------------------------
348
  # st.markdown("<div class='sectionheader'>Crime Hotspots by Region NO MAP</div>", unsafe_allow_html=True)