본문 바로가기
Streamlit

[Streamlit] 차트 그리기

by eyoo 2022. 5. 20.
스트림릿에서 차트를 그릴수있다.
 
sepal_length와 sepal_width의 관계를 scatter 차트로 나타내보자
 
in:
    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)
out:
# 스트림릿에서 pyplot이나 seaborn 차트를 나타낼때 plt.show 함수대신 st.pyplot 함수를 사용한다.

 

 
regplot을 사용해보자
 
 
in:
fig3 = plt.figure()
    sns.regplot(data= df, x='sepal_length', y= 'sepal_width')
    st.pyplot(fig3)
out:
# 꽃받침의 너비와 길이는 별 관계 없어 보인다.
 
 
히스토그램을 만들고 bin의 개수는 20개로 설정한다.
 
in:
    fig4 = plt.figure()
    plt.hist(data= df, x='sepal_length',bins=20, rwidth=0.8)
    st.pyplot(fig4)
   
 out:
 
 
sepal_length의 히스토그램을 그리되 bin의 개수를 10개와 20개로 두개의 차트를 수평으로 보여주자

in:
    fig5 = plt.figure(figsize=(12,5))

    plt.subplot(1,2,1)
    plt.hist(data=df,x='sepal_length',rwidth=0.8, bins = 10)

    plt.subplot(1,2,2)
    plt.hist(data=df,x='sepal_length',rwidth=0.8, bins = 20)

    st.pyplot(fig5)​

out:
# subplot을 활용하여 두개의 차트를 하나의 영역에서 보여줬다.

 

 
species는 각각 몇개인지 데이터 프레임의 차트로 그린다.
 
in:
    fig7 = plt.figure()
    df['species'].value_counts().plot(kind='bar')
    st.pyplot(fig7)

out:

# 균등하게 분포되었다.

 

 

sepal_length 컬럼을 히스토그램으로 나타내자.
 
in:
    fig8 = plt.figure()
    df['sepal_length'].hist(bins=40)
    st.pyplot(fig8)​

out:
 
 
 
 
 
 

댓글