본문 바로가기
APP/Streamlit

[Streamlit] Streamlit을 사용한 붓꽃 데이터 시각화 프로젝트

by Foxy현 2023. 8. 9.
728x90
반응형

이 글에서는 Streamlit과 Seaborn 라이브러리를 활용하여 붓꽃(iris) 데이터를 시각화하는 방법을 소개합니다.

붓꽃 데이터 로드 및 웹 앱 구조 설정

_, col, _ = st.columns([2,6,2])
col.header('Streamlit 시각화')
iris_df = sns.load_dataset('iris')

 

사이드바를 활용한 변수 및 옵션 선택

사용자는 사이드바에서 X 축과 Y 축 변수를 선택하며, 특정 붓꽃 유형 및 그래프의 투명도(alpha)도 설정할 수 있습니다.

with st.sidebar:
    selectX = st.selectbox('X 변수 선택:', ['sepal_length','sepal_width','petal_length','petal_width'])
    selectY = st.selectbox('Y 변수 선택:', ['sepal_length','sepal_width','petal_length','petal_width'])
    selectSpecies = st.multiselect('붓꽃 유형 선택:', ['setosa','versicolor','virginica'])
    selectAlpha = st.slider('alpha 설정:', 0.1, 1.0, 0.5)

선택한 옵션을 바탕으로 scatter plot 생성

선택한 변수와 옵션을 기반으로 Matplotlib을 활용하여 scatter plot을 그립니다.

 

colors = {'setosa':'red', 'versicolor':'orange','virginica':'green'}

if selectSpecies:
    fig = plt.figure(figsize=(7,5))
    for aSpecies in selectSpecies:
        df = iris_df[iris_df.species==aSpecies]
        plt.scatter(df[selectX], df[selectY], color=colors[aSpecies], alpha=selectAlpha, label=aSpecies)
    plt.xlabel(selectX)
    plt.ylabel(selectY)
    plt.title('iris scatter plot')
    st.pyplot(fig)

 

옵션 미선택 시 경고 메시지 출력

else:
    st.warning('붓꽃의 유형을 선택해주세요')

 

결과물


간단한 시각화 프로젝트를 진행해보았습니다.

streamlim 정말 엄청나게 편한 개발 툴이네요..

 

 

 

728x90
반응형