Streamlit

python(파이썬)의 streamlit(스트림릿)에서 다양한 데이터 차트 그리기 1

HooSL 2022. 1. 10. 12:17

스트림릿과 차트그리기에 필요한 라이브러리 import 해줍니다.

import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

스트림릿 화면 출력 함수를 작성해주시고

def main():
	pass #pass부분에 작성해주시면 됩니다.
if __name__ == '__main__':
    main()

데이터프레임과 타이틀 이름 정해주기

    st.title('Plotting with st.pyplot()')

    df = pd.read_csv('data/iris.csv')

    st.dataframe(df.head())

    # sepal_length와 sepal_width의 관계를 차트로 그립니다.
    fig = plt.figure()
    plt.scatter(data=df,x='sepal_length',y='sepal_width')
    plt.title('sepal_length vs width')
    plt.xlabel('sepal_length')
    plt.ylabel('sepal_width')
    st.pyplot(fig)

    fig2 = plt.figure()
    sns.regplot(data=df,x='sepal_length',y='sepal_width')
    st.pyplot(fig2)

    # sepal_length로 히스토그램을 그리기
    # bin의 개수는 20개로 하겠습니다.
    fig3 = plt.figure(figsize=(10,4))
    plt.subplot(1,2,1)
    plt.hist(data=df,x='sepal_length',bins=10,rwidth=0.8)
    plt.subplot(1,2,2)
    plt.hist(data=df,x='sepal_length',bins=20,rwidth=0.8)
    st.pyplot(fig3)

    # species 컬럼에는 종 정보가 들어있는데 각 종별로 몇개씩 있는지를 차트로 나타내기
    fig4 = plt.figure()
    sns.countplot(data=df,x='species')
    st.pyplot(fig4)

    fig5 = plt.figure()
    df['species'].value_counts().plot(kind = 'bar')
    st.pyplot(fig5)

    fig6 = plt.figure()
    df['sepal_length'].hist()
    st.pyplot(fig6)