File size: 2,292 Bytes
e453eff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

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",
)