Commit ·
cb8a883
1
Parent(s): bfae6d2
:sparkles: Add county filter and highlight selected county in charts
Browse filesThis update introduces an interactive county selection filter to the app, allowing users to choose a specific county or view all data at once. The selected county is visually highlighted in the bar chart to enhance data interpretation, ensuring a more intuitive and engaging user experience.
app.py
CHANGED
|
@@ -38,19 +38,37 @@ st.subheader("Explore EV Distribution by County")
|
|
| 38 |
county_counts = ev_data['County'].value_counts().reset_index()
|
| 39 |
county_counts.columns = ['County', 'Count']
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
fig_county = px.bar(
|
| 42 |
county_counts,
|
| 43 |
x='County',
|
| 44 |
y='Count',
|
| 45 |
-
title="Electric Vehicle Counts by County",
|
| 46 |
labels={"Count": "Number of EVs", "County": "County"},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
height=500
|
| 48 |
)
|
| 49 |
st.plotly_chart(fig_county, use_container_width=True)
|
| 50 |
|
| 51 |
# Contextual Visualization 1: Vehicle Model Trends
|
| 52 |
ev_data['Model Year'] = pd.to_numeric(ev_data['Model Year'], errors='coerce')
|
| 53 |
-
model_year_counts =
|
| 54 |
model_year_counts.columns = ['Year', 'Count']
|
| 55 |
model_year_counts = model_year_counts.sort_values('Year')
|
| 56 |
|
|
@@ -66,7 +84,7 @@ st.plotly_chart(fig_year, use_container_width=True)
|
|
| 66 |
|
| 67 |
# Contextual Visualization 2: Map of EV Locations
|
| 68 |
st.subheader("Geographic Distribution of EVs")
|
| 69 |
-
filtered_data =
|
| 70 |
st.pydeck_chart(pdk.Deck(
|
| 71 |
initial_view_state=pdk.ViewState(
|
| 72 |
latitude=filtered_data['Latitude'].mean(),
|
|
|
|
| 38 |
county_counts = ev_data['County'].value_counts().reset_index()
|
| 39 |
county_counts.columns = ['County', 'Count']
|
| 40 |
|
| 41 |
+
# Add interactivity for county selection
|
| 42 |
+
selected_county = st.selectbox("Select a County to Filter:", options=["All"] + county_counts['County'].tolist())
|
| 43 |
+
if selected_county != "All":
|
| 44 |
+
ev_data_filtered = ev_data[ev_data['County'] == selected_county]
|
| 45 |
+
else:
|
| 46 |
+
ev_data_filtered = ev_data
|
| 47 |
+
|
| 48 |
+
# Highlight the selected bar
|
| 49 |
+
county_counts['Color'] = county_counts['County'].apply(
|
| 50 |
+
lambda x: 'blue' if x == selected_county else 'green'
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Contextual Visualization 0: EV Bar Counts by County
|
| 54 |
fig_county = px.bar(
|
| 55 |
county_counts,
|
| 56 |
x='County',
|
| 57 |
y='Count',
|
| 58 |
+
title=f"Electric Vehicle Counts by {selected_county if selected_county != 'All' else 'County'}",
|
| 59 |
labels={"Count": "Number of EVs", "County": "County"},
|
| 60 |
+
color='County',
|
| 61 |
+
color_discrete_map={
|
| 62 |
+
selected_county: 'blue',
|
| 63 |
+
'Other': 'green'
|
| 64 |
+
},
|
| 65 |
height=500
|
| 66 |
)
|
| 67 |
st.plotly_chart(fig_county, use_container_width=True)
|
| 68 |
|
| 69 |
# Contextual Visualization 1: Vehicle Model Trends
|
| 70 |
ev_data['Model Year'] = pd.to_numeric(ev_data['Model Year'], errors='coerce')
|
| 71 |
+
model_year_counts = ev_data_filtered['Model Year'].value_counts().reset_index()
|
| 72 |
model_year_counts.columns = ['Year', 'Count']
|
| 73 |
model_year_counts = model_year_counts.sort_values('Year')
|
| 74 |
|
|
|
|
| 84 |
|
| 85 |
# Contextual Visualization 2: Map of EV Locations
|
| 86 |
st.subheader("Geographic Distribution of EVs")
|
| 87 |
+
filtered_data = ev_data_filtered.dropna(subset=['Latitude', 'Longitude'])
|
| 88 |
st.pydeck_chart(pdk.Deck(
|
| 89 |
initial_view_state=pdk.ViewState(
|
| 90 |
latitude=filtered_data['Latitude'].mean(),
|