Update src/streamlit_app.py
Browse files- src/streamlit_app.py +70 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,72 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import altair as alt
|
| 4 |
+
|
| 5 |
+
st.set_page_config(layout="wide", page_title="BFRO Sightings Analysis")
|
| 6 |
+
|
| 7 |
+
st.title("BFRO Sightings Dashboard")
|
| 8 |
+
st.markdown("### Exploration of Bigfoot Reports using Streamlit and Altair")
|
| 9 |
+
st.write("This dashboard visualizes reports from the BFRO database, focusing on geographic distribution and seasonal patterns.")
|
| 10 |
+
|
| 11 |
+
@st.cache_data
|
| 12 |
+
def load_data():
|
| 13 |
+
url = "https://raw.githubusercontent.com/UIUC-iSchool-DataViz/is445_data/main/bfro_reports_fall2022.csv"
|
| 14 |
+
df = pd.read_csv(url)
|
| 15 |
+
df_clean = df.dropna(subset=['season', 'state', 'latitude', 'longitude'])
|
| 16 |
+
return df_clean
|
| 17 |
+
|
| 18 |
+
df_clean = load_data()
|
| 19 |
+
|
| 20 |
+
alt.data_transformers.disable_max_rows()
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
season_dropdown = alt.binding_select(options=df_clean['season'].unique().tolist(), name="Select Season: ")
|
| 24 |
+
season_select = alt.selection_point(fields=['season'], bind=season_dropdown)
|
| 25 |
+
|
| 26 |
+
brush = alt.selection_interval()
|
| 27 |
+
|
| 28 |
+
map_chart = alt.Chart(df_clean).mark_rect().encode(
|
| 29 |
+
x=alt.X('longitude:Q', bin=alt.Bin(maxbins=60), title='Longitude'),
|
| 30 |
+
y=alt.Y('latitude:Q', bin=alt.Bin(maxbins=40), title='Latitude'),
|
| 31 |
+
color=alt.Color('count()', scale=alt.Scale(scheme='inferno'), title='Report Density'),
|
| 32 |
+
tooltip=['count()']
|
| 33 |
+
).add_params(
|
| 34 |
+
brush, season_select
|
| 35 |
+
).transform_filter(
|
| 36 |
+
season_select
|
| 37 |
+
).properties(
|
| 38 |
+
width=400,
|
| 39 |
+
height=400,
|
| 40 |
+
title='1. Geographic Density (Drag to Select)'
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
heatmap = alt.Chart(df_clean).mark_bar().encode(
|
| 44 |
+
x=alt.X('count()', title='Total Reports'),
|
| 45 |
+
y=alt.Y('state:N', title='State', sort='-x'),
|
| 46 |
+
color=alt.Color('season:N', title='Season'),
|
| 47 |
+
tooltip=['state', 'season', 'count()']
|
| 48 |
+
).transform_filter(
|
| 49 |
+
brush
|
| 50 |
+
).properties(
|
| 51 |
+
width=300,
|
| 52 |
+
height=400,
|
| 53 |
+
title='2. Reports by State (Filtered by Map)'
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
dashboard = map_chart | heatmap
|
| 57 |
+
|
| 58 |
+
st.altair_chart(dashboard, use_container_width=True)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
st.divider()
|
| 62 |
+
st.header("Analysis & Write-up")
|
| 63 |
+
|
| 64 |
+
st.subheader("Visualization 1: Geographic Density Map")
|
| 65 |
+
st.markdown("""
|
| 66 |
+
This visualization highlights the spatial distribution of Bigfoot reports across the United States. I chose a **rectangular binning** approach (`mark_rect`) rather than plotting individual points to better handle the data density and avoid overplotting in high-activity areas like the Pacific Northwest. The **'inferno' color scheme** was selected to provide high contrast, where lighter colors immediately draw the viewer's eye to areas of high report density. This plot is interactive; it includes a dropdown to filter by season, allowing users to analyze whether sighting locations shift during different times of the year, and it acts as a filter for the second chart. **If I had more time**, I would overlay these bins onto a geographic base map (using `mark_geoshape`) to provide better context regarding state borders and physical geography.
|
| 67 |
+
""")
|
| 68 |
|
| 69 |
+
st.subheader("Visualization 2: Reports by State")
|
| 70 |
+
st.markdown("""
|
| 71 |
+
This bar chart highlights the frequency of reports aggregated by state. I used a **bar mark** as it effectively compares magnitudes across categories (states). The states are **sorted in descending order** of report counts to instantly reveal the most active locations without requiring the user to scan the entire axis. The color encoding represents the 'season', providing a secondary dimension of information consistent with the map. This visualization is linked to the map via a brush filter; dragging a selection box on the map dynamically updates this bar chart to show the state breakdown for only the selected region. **If I had more time**, I would normalize the data by state population or land area to provide a per-capita or per-square-mile perspective, which might reveal different hotspots than raw counts alone.
|
| 72 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|