vgosavi2 commited on
Commit
8f9f24e
·
verified ·
1 Parent(s): 48e53e1

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # -------------------------------- Plot 7: Bubble Map of Incident Counts by Region NO MAP --------------------------------
273
- st.markdown("<div class='sectionheader'>Crime Hotspots by Region NO MAP</div>", unsafe_allow_html=True)
274
-
275
- # 1. Aggregate total incidents by region and pick top 10
276
- region_counts = (
277
  df
278
- .groupby("RegionName") # group by your text field
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
- # 2. Compute circle‐packing layout
317
- circles = circlify.circlify(
318
- region_counts["Count"].tolist(),
319
- show_enclosure=False,
320
- target_enclosure=circlify.Circle(x=0, y=0, r=0.5)
 
 
321
  )
 
 
322
 
323
- # 3. Plot packed bubbles
324
- fig, ax = plt.subplots(figsize=(3, 3))
325
- ax.axis("off")
326
 
327
- # Determine plot limits so everything fits
328
- lim = max(
329
- abs(circle.x) + circle.r
330
- for circle in circles
331
- )
332
- ax.set_xlim(-lim, lim)
333
- ax.set_ylim(-lim, lim)
334
-
335
- # Draw each circle
336
- for circle, (_, row) in zip(circles, region_counts.iterrows()):
337
- x, y, r = circle.x, circle.y, circle.r
338
- ax.add_patch(plt.Circle((x, y), r, alpha=0.6, linewidth=2, ec="white"))
339
- ax.text(
340
- x, y,
341
- f"{row['RegionName']}\n{row['Count']:,}",
342
- ha="center", va="center", fontsize=8, color="#222"
343
- )
344
-
345
- ax.set_title("Packed Bubble: Top 15 Regions by Incident Count", fontsize=12, pad=10)
346
-
347
- # 4. Render in Streamlit
348
- st.pyplot(fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
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)