Update src/streamlit_app.py
Browse files- src/streamlit_app.py +37 -26
src/streamlit_app.py
CHANGED
|
@@ -88,35 +88,46 @@ chart1 = (
|
|
| 88 |
)
|
| 89 |
st.altair_chart(chart1, use_container_width=True)
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
)
|
|
|
|
| 104 |
)
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
)
|
| 115 |
|
| 116 |
-
#
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
)
|
| 121 |
|
| 122 |
-
|
|
|
|
|
|
| 88 |
)
|
| 89 |
st.altair_chart(chart1, use_container_width=True)
|
| 90 |
|
| 91 |
+
###############################################################
|
| 92 |
+
|
| 93 |
+
import plotly.express as px
|
| 94 |
+
|
| 95 |
+
# 4. Pie Chart 1: Top 10 Crime Types
|
| 96 |
+
st.header("Pie Chart 1: Top 10 Crime Types Distribution")
|
| 97 |
+
|
| 98 |
+
# Prepare Top 10 crime types
|
| 99 |
+
top_crimes = (
|
| 100 |
+
df["crm_cd_desc"]
|
| 101 |
+
.value_counts()
|
| 102 |
+
.nlargest(10)
|
| 103 |
+
.rename_axis("Crime Type")
|
| 104 |
+
.reset_index(name="Count")
|
| 105 |
)
|
| 106 |
+
# Compute percentages
|
| 107 |
+
top_crimes["Percentage"] = top_crimes["Count"] / top_crimes["Count"].sum()
|
| 108 |
|
| 109 |
+
# Build Plotly pie chart
|
| 110 |
+
fig = px.pie(
|
| 111 |
+
top_crimes,
|
| 112 |
+
names="Crime Type",
|
| 113 |
+
values="Count",
|
| 114 |
+
hole=0.4, # makes it a donut
|
| 115 |
+
hover_data=["Percentage"], # show percentage on hover
|
| 116 |
+
title="Top 10 Crime Types"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Move labels outside slices and show both label & percent
|
| 120 |
+
fig.update_traces(
|
| 121 |
+
textposition="outside",
|
| 122 |
+
textinfo="label+percent",
|
| 123 |
+
marker=dict(line=dict(color="#000000", width=1)) # add slice borders
|
| 124 |
)
|
| 125 |
|
| 126 |
+
# Style the legend
|
| 127 |
+
fig.update_layout(
|
| 128 |
+
legend_title_text="Crime Type",
|
| 129 |
+
margin=dict(t=50, l=25, r=25, b=25)
|
| 130 |
)
|
| 131 |
|
| 132 |
+
# Render in Streamlit
|
| 133 |
+
st.plotly_chart(fig, use_container_width=True)
|