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 streamlit as st import altair as alt import seaborn as sns import pandas as pd st.title("🐧 Palmer Penguins Explorer") # 载入数据 penguins = sns.load_dataset("penguins") penguins_clean = penguins.dropna(subset=[ "bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g", "species" ]) # 侧边栏选择 x_col = st.sidebar.selectbox("X 轴", ["bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g"]) y_col = st.sidebar.selectbox("Y 轴", ["flipper_length_mm", "bill_length_mm", "bill_depth_mm", "body_mass_g"]) size_col = st.sidebar.selectbox("点大小", ["body_mass_g", "flipper_length_mm", "bill_length_mm"]) color_col = st.sidebar.selectbox("颜色分组", ["species", "island", "sex"]) st.write(f"**X 轴**:`{x_col}`,**Y 轴**:`{y_col}`,**大小**:`{size_col}`,**颜色**:`{color_col}`") chart = ( alt.Chart(penguins_clean) .mark_point(filled=True) .encode( alt.X(x_col, title=x_col.replace("_", " ").title()), alt.Y(y_col, title=y_col.replace("_", " ").title()), alt.Size(size_col, title=size_col.replace("_", " ").title(), scale=alt.Scale(range=[20, 300])), alt.Color(color_col, title=color_col.replace("_", " ").title()), alt.OpacityValue(0.7), tooltip=[ alt.Tooltip("species", title="Species"), alt.Tooltip("island", title="Island"), alt.Tooltip("sex", title="Sex"), alt.Tooltip("bill_length_mm", title="Bill Length (mm)"), alt.Tooltip("bill_depth_mm", title="Bill Depth (mm)"), alt.Tooltip("flipper_length_mm", title="Flipper Length (mm)"), alt.Tooltip("body_mass_g", title="Body Mass (g)"), ], ) .properties(width=750, height=460, title="Palmer Penguins — Interactive Scatter") .interactive() ) st.altair_chart(chart, use_container_width=True) st.subheader("原始数据(清洗后)") st.dataframe(penguins_clean)