|
|
| import streamlit as st
|
| import pandas as pd
|
|
|
|
|
| import seaborn as sns
|
| import matplotlib.pyplot as plt
|
|
|
|
|
| sns.set_theme()
|
| sns.set(color_codes=True)
|
|
|
|
|
|
|
| penguins = sns.load_dataset("penguins")
|
|
|
|
|
| st.title("Differences between penguins")
|
|
|
| st.subheader("My flipper is longer!!!")
|
|
|
|
|
| st.image("https://raw.githubusercontent.com/allisonhorst/palmerpenguins/main/man/figures/lter_penguins.png")
|
|
|
|
|
| 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]
|
|
|
|
|
| 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:
|
| data = penguins[penguins["species"].isin(selected_species)].dropna(subset=[x_col])
|
|
|
| g = sns.displot(
|
| data=data,
|
| x=x_col,
|
| kind="kde",
|
| hue="species",
|
| fill=fill,
|
| bw_adjust=bw_adjust,
|
| common_norm=common_norm,
|
| height=4,
|
| aspect=1.6,
|
| )
|
| fig = g.fig if hasattr(g, "fig") else g.figure
|
| st.pyplot(fig)
|
| plt.close(fig)
|
|
|
|
|
| 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",
|
| )
|
|
|
|
|
| st.caption("Developed for SDS M1 course.")
|
|
|