Ailee52 commited on
Commit
b0fa05e
·
verified ·
1 Parent(s): 540c6f1

Upload 2 files

Browse files
Files changed (2) hide show
  1. app1.py +124 -0
  2. requirements.txt +0 -0
app1.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+
3
+ # in some cases ipynb needs the line bellow in order to correctly show the plots
4
+ #matplotlib inline
5
+ import seaborn as sns
6
+ import matplotlib.pyplot as plt
7
+
8
+ # Apply the default theme and activate color codes
9
+ sns.set_theme()
10
+ #sns.set(color_codes=True)
11
+
12
+ # load a dataset
13
+ penguins = sns.load_dataset("penguins")
14
+ penguins.head()
15
+
16
+ sns.displot(data=penguins,
17
+ x="flipper_length_mm")
18
+ sns.displot(data=penguins,
19
+ x="flipper_length_mm",
20
+ kind="kde")
21
+
22
+ sns.displot(data=penguins,
23
+ x="flipper_length_mm",
24
+ kind="kde",
25
+ hue="species")
26
+
27
+ #%%writefile app1.py
28
+
29
+ import streamlit as st
30
+ import pandas as pd
31
+
32
+
33
+ import seaborn as sns
34
+ import matplotlib.pyplot as plt
35
+
36
+ # Apply the default theme and activate color codes
37
+ sns.set_theme()
38
+ #sns.set(color_codes=True)
39
+
40
+ #%%writefile -a app1.py
41
+
42
+ ################### import dataset
43
+ penguins = sns.load_dataset("penguins")
44
+
45
+ #%%writefile -a app1.py
46
+
47
+ ########### set the title and subtitle
48
+ st.title("Differences between penguins")
49
+
50
+ st.subheader("My flipper is longer!!!")
51
+
52
+ #%%writefile -a app1.py
53
+
54
+ ########## we add image
55
+ st.image("https://raw.githubusercontent.com/allisonhorst/palmerpenguins/main/man/figures/lter_penguins.png")
56
+
57
+ #%%writefile -a app1.py
58
+
59
+ ############## we create filters for our interactive plot
60
+ with st.sidebar:
61
+ st.subheader("Filters")
62
+
63
+ all_species = sorted(penguins["species"].dropna().unique().tolist())
64
+ selected_species = st.multiselect(
65
+ "Species to show",
66
+ options=all_species,
67
+ default=all_species,
68
+ )
69
+
70
+ feature_options = {
71
+ "Flipper length (mm)": "flipper_length_mm",
72
+ "Bill length (mm)": "bill_length_mm",
73
+ "Bill depth (mm)": "bill_depth_mm",
74
+ "Body mass (g)": "body_mass_g",
75
+ }
76
+ feature_label = st.selectbox("Feature (x-axis)", list(feature_options.keys()))
77
+ x_col = feature_options[feature_label]
78
+
79
+ # KDE options
80
+ fill = st.checkbox("Shade area", value=True)
81
+ bw_adjust = st.slider("Smoothing (bw_adjust)", 0.2, 2.0, 1.0, 0.1)
82
+ common_norm = st.checkbox("Normalize across species", value=False)
83
+
84
+ #%%writefile -a app1.py
85
+
86
+ if not selected_species:
87
+ st.info("Select at least one species to display the plot.")
88
+ else:
89
+ data = penguins[penguins["species"].isin(selected_species)].dropna(subset=[x_col])
90
+
91
+ g = sns.displot(
92
+ data=data,
93
+ x=x_col,
94
+ kind="kde",
95
+ hue="species",
96
+ fill=fill,
97
+ bw_adjust=bw_adjust,
98
+ common_norm=common_norm,
99
+ height=4,
100
+ aspect=1.6,
101
+ )
102
+ fig = g.fig if hasattr(g, "fig") else g.figure
103
+ st.pyplot(fig)
104
+ plt.close(fig)
105
+
106
+ #%%writefile -a app.py
107
+
108
+ ######## add button to save image
109
+ from io import BytesIO
110
+
111
+ buf = BytesIO()
112
+ fig.savefig(buf, format="png", dpi=200, bbox_inches="tight")
113
+ buf.seek(0)
114
+ st.download_button(
115
+ "Save image",
116
+ data=buf,
117
+ file_name=f"penguins_{x_col}.png",
118
+ mime="image/png",
119
+ )
120
+
121
+ #%%writefile -a app.py
122
+
123
+ ########### Footer
124
+ st.caption("Developed for SDS M1 course.")
requirements.txt ADDED
File without changes