TimoPh's picture
Upload 2 files
e453eff verified
import streamlit as st
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Apply the default theme and activate color codes
sns.set_theme()
sns.set(color_codes=True)
################### import dataset
penguins = sns.load_dataset("penguins")
########### set the title and subtitle
st.title("Differences between penguins")
st.subheader("My flipper is longer!!!")
########## we add image
st.image("https://raw.githubusercontent.com/allisonhorst/palmerpenguins/main/man/figures/lter_penguins.png")
############## we create filters for our interactive plot
with st.sidebar:
st.subheader("Filters")
all_species = sorted(penguins["species"].dropna().unique().tolist())
selected_species = st.multiselect(
"Species to show",
options=all_species,
default=all_species,
)
feature_options = {
"Flipper length (mm)": "flipper_length_mm",
"Bill length (mm)": "bill_length_mm",
"Bill depth (mm)": "bill_depth_mm",
"Body mass (g)": "body_mass_g",
}
feature_label = st.selectbox("Feature (x-axis)", list(feature_options.keys()))
x_col = feature_options[feature_label]
# KDE options
fill = st.checkbox("Shade area", value=True)
bw_adjust = st.slider("Smoothing (bw_adjust)", 0.2, 2.0, 1.0, 0.1)
common_norm = st.checkbox("Normalize across species", value=False)
if not selected_species:
st.info("Select at least one species to display the plot.")
else:
# Filter the data
data = penguins[penguins["species"].isin(selected_species)].dropna(subset=[x_col, "bill_length_mm"])
# Plot using the selected x-axis
g = sns.relplot(
data=data,
x=x_col, # dynamic x-axis
y="bill_length_mm", # you could also make y selectable similarly
kind="scatter",
hue="species"
)
fig = g.fig if hasattr(g, "fig") else g.figure
st.pyplot(fig)
plt.close(fig)
######## add button to save image
from io import BytesIO
buf = BytesIO()
fig.savefig(buf, format="png", dpi=200, bbox_inches="tight")
buf.seek(0)
st.download_button(
"Save image",
data=buf,
file_name=f"penguins_{x_col}.png",
mime="image/png",
)