본문 바로가기
Streamlit

Streamlit 기본 문법(5) : radio(라디오) 위젯

by ISLA! 2023. 7. 28.

라디오 위젯 사용하기

 

3개의 라디오 버튼을 만들고, 서로 다른 유형의 그래프를 보여주는 코드

def plot_matplotlib():
    st.title('산점도 Matplotlib')
    fig, ax = plt.subplots()
    ax.scatter(iris['sepal_length'], iris['sepal_width'])
    st.pyplot(fig)

def plot_seaborn():
    st.title('산점도 Seaborn')
    fig, ax = plt.subplots()
    sns.scatterplot(iris, x = 'sepal_length', y ='sepal_width')
    st.pyplot(fig)

def plot_plotly():
    st.title('Scatter Plot with Plotly')
    fig = go.Figure()
    fig.add_trace(
        go.Scatter(x = iris['sepal_length'],
                   y = iris['sepal_width'],
                   mode='markers')
    )
    st.plotly_chart(fig)

#세가지 타입의 산점도 보여주기(라디오 위젯)
st.write('-' * 50)
plot_type = st.radio(
    "어떤 스타일의 산점도를 보고 싶으세요?",
    ("matplotlib", "seaborn", "plotly")
)
st.write(plot_type)

if plot_type == "matplotlib":
    plot_matplotlib()
elif plot_type == "seaborn":
    plot_seaborn()
elif plot_type == "plotly":
    plot_plotly()
else:
    st.error("에러입니다.")


if __name__ == "__main__":
    main()

 

👉 결과 : 각각의 라디오박스 내용에 해당하는 라이브러리로 그려진 그래프가 나타남

728x90