vgosavi2 commited on
Commit
8bd5abd
·
verified ·
1 Parent(s): 6ae230d

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +40 -0
src/streamlit_app.py CHANGED
@@ -228,6 +228,46 @@ criminal threats and minor burglary. Violent acts such as simple assault and rob
228
  relying on specific numbers. By pairing each slice with its label, the chart provides an immediate intuitive understanding of which crime types contribute most to overall
229
  volume and which are comparatively rare, helping stakeholders focus on the offenses that matter most.</div>""",unsafe_allow_html=True)
230
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  # -------------------------------- Plot 2: Heat Map --------------------------------
232
  st.markdown("<div class='sectionheader'> HeatMap </div>", unsafe_allow_html=True)
233
  # Count the crime type and list out the top 10 crime type that have the most cases.
 
228
  relying on specific numbers. By pairing each slice with its label, the chart provides an immediate intuitive understanding of which crime types contribute most to overall
229
  volume and which are comparatively rare, helping stakeholders focus on the offenses that matter most.</div>""",unsafe_allow_html=True)
230
 
231
+ # -------------------------------- Plot 6: Bubble Map of Incident Counts by Region --------------------------------
232
+ st.markdown("<div class='sectionheader'>Crime Hotspots by Region</div>", unsafe_allow_html=True)
233
+
234
+ # 1. Aggregate counts and centroids
235
+ region_stats = (
236
+ df
237
+ .groupby("RegionName")
238
+ .agg(
239
+ Count = pd.NamedAgg(column="crm_cd_desc", aggfunc="size"),
240
+ Latitude = pd.NamedAgg(column="lat", aggfunc="mean"),
241
+ Longitude = pd.NamedAgg(column="lon", aggfunc="mean")
242
+ )
243
+ .reset_index()
244
+ )
245
+
246
+ # 2. Build the bubble map
247
+ fig = px.scatter_mapbox(
248
+ region_stats,
249
+ lat="Latitude",
250
+ lon="Longitude",
251
+ size="Count", # bubble size ~ incident volume
252
+ color="Count", # color gradient for emphasis
253
+ hover_name="RegionName",
254
+ hover_data={"Count":True, "Latitude":False, "Longitude":False},
255
+ size_max=30, # max bubble diameter
256
+ zoom=10, # adjust to focus your city
257
+ mapbox_style="open-street-map",
258
+ title="Crime Volume by Region (Bubble Map)"
259
+ )
260
+
261
+ # 3. Tidy layout
262
+ fig.update_layout(
263
+ margin=dict(t=50, b=0, l=0, r=0),
264
+ legend_title_text="Incident Count",
265
+ title_x=0.5
266
+ )
267
+
268
+ # 4. Render
269
+ st.plotly_chart(fig, use_container_width=True)
270
+
271
  # -------------------------------- Plot 2: Heat Map --------------------------------
272
  st.markdown("<div class='sectionheader'> HeatMap </div>", unsafe_allow_html=True)
273
  # Count the crime type and list out the top 10 crime type that have the most cases.