Python/Matplotlib

scatter, histogram ๋“ฑ์˜ ์ฐจํŠธ๋ฅผ ์ŠคํŠธ๋ฆผ๋ฆฟ(streamlit)์œผ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ

567Rabbit 2024. 4. 26. 17:57

 

python
๋‹ซ๊ธฐ
# ์ฐจํŠธ ๊ทธ๋ฆฌ๊ธฐ ๋ฐฉ๋ฒ• import streamlit as st import pandas as pd import matplotlib.pyplot as plt import seaborn as sb def main(): โ€‹โ€‹โ€‹โ€‹st.title('์ฐจํŠธ ๊ทธ๋ฆฌ๊ธฐ 1') โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹df = pd.read_csv('./data/iris.csv') โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹st.dataframe(df) โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹# sepal_length ์™€ sepal_width์˜ ๊ด€๊ณ„๋ฅผ ์ฐจํŠธ๋กœ ๋‚˜ํƒ€๋‚ด์‹œ์˜ค โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹fig1 = plt.figure() โ€‹โ€‹โ€‹โ€‹plt.scatter(data=df, x='sepal_length', y='sepal_width') โ€‹โ€‹โ€‹โ€‹plt.title('Sepal Length vs Width') โ€‹โ€‹โ€‹โ€‹st.pyplot(fig1) โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹fig2 = plt.figure() โ€‹โ€‹โ€‹โ€‹sb.scatterplot(data=df, x='sepal_length', y='sepal_width') โ€‹โ€‹โ€‹โ€‹plt.title('Sepal Length vs Width') โ€‹โ€‹โ€‹โ€‹st.pyplot(fig2) โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹fig3 = plt.figure() โ€‹โ€‹โ€‹โ€‹sb.regplot(data=df, x='sepal_length', y='sepal_width') โ€‹โ€‹โ€‹โ€‹plt.title('Sepal Length vs Width') โ€‹โ€‹โ€‹โ€‹st.pyplot(fig3) โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹# sepal_length๋กœ ํžˆ์Šคํ† ๊ทธ๋žจ์„ ๊ทธ๋ฆฐ๋‹ค โ€‹โ€‹โ€‹โ€‹# bins์˜ ๊ฐœ์ˆ˜๋Š” 20๊ฐœ๋กœ โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹fig4 = plt.figure() โ€‹โ€‹โ€‹โ€‹plt.hist(data=df, x='sepal_length', bins=20, rwidth=0.9) โ€‹โ€‹โ€‹โ€‹plt.title('Histogram') โ€‹โ€‹โ€‹โ€‹plt.xlabel('sepal_length') โ€‹โ€‹โ€‹โ€‹plt.ylabel('count') โ€‹โ€‹โ€‹โ€‹st.pyplot(fig4) โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹# sepal_length๋กœ ํžˆ์Šคํ† ๊ทธ๋žจ์„ ๊ทธ๋ฆฌ๋˜, bins ๊ฐœ์ˆ˜๋ฅผ 10๊ฐœ์™€ 20๊ฐœ๋กœ ๋‘๊ฐœ์˜ ์ฐจํŠธ๋ฅผ ์ˆ˜ํ‰์œผ๋กœ ๋ณด์—ฌ์ฃผ์„ธ์š” โ€‹โ€‹โ€‹โ€‹fig5 = plt.figure(figsize=(10,4)) โ€‹โ€‹โ€‹โ€‹plt.subplot(1, 2, 1) โ€‹โ€‹โ€‹โ€‹plt.hist(data=df, x='sepal_length', bins=10, rwidth=0.9) โ€‹โ€‹โ€‹โ€‹plt.title('Histogram') โ€‹โ€‹โ€‹โ€‹plt.xlabel('sepal_length') โ€‹โ€‹โ€‹โ€‹plt.ylabel('count') โ€‹โ€‹โ€‹โ€‹plt.subplot(1, 2, 2) โ€‹โ€‹โ€‹โ€‹plt.hist(data=df, x='sepal_length', bins=20, rwidth=0.9) โ€‹โ€‹โ€‹โ€‹plt.title('Histogram') โ€‹โ€‹โ€‹โ€‹plt.xlabel('sepal_length') โ€‹โ€‹โ€‹โ€‹plt.ylabel('count') โ€‹โ€‹โ€‹โ€‹st.pyplot(fig5) โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹# ํŒ๋‹ค์Šค์˜ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์˜ ์ฐจํŠธ๋กœ ๊ทธ๋ฆด์ˆ˜ ์žˆ๋‹ค โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹# species๋Š” ๊ฐ๊ฐ ๋ช‡๊ฐœ์ธ์ง€ ๋‚˜ํƒ€๋‚ด์‹œ์˜ค โ€‹โ€‹โ€‹โ€‹print(df['species'].value_counts()) โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹# ์œ„์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”์ฐจํŠธ๋กœ ๋‚˜ํƒ€๋‚ด์‹œ์˜ค โ€‹โ€‹โ€‹โ€‹fig7 = plt.figure() โ€‹โ€‹โ€‹โ€‹df['species'].value_counts().plot(kind='bar') โ€‹โ€‹โ€‹โ€‹st.pyplot(fig7) โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹# sepal_length ์ปฌ๋Ÿผ์„ ํžˆ์Šคํ† ๊ทธ๋žจ์œผ๋กœ ๋‚˜ํƒ€๋‚ด์‹œ์˜ค โ€‹โ€‹โ€‹โ€‹fig8 = plt.figure() โ€‹โ€‹โ€‹โ€‹df['sepal_length'].hist() โ€‹โ€‹โ€‹โ€‹st.pyplot(fig8) โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹# df์˜ ์ƒ๊ด€๊ณ„์ˆ˜๋ฅผ ๊ตฌํ•ด์„œ, ์ฐจํŠธ๋กœ ํ‘œ์‹œ! โ€‹โ€‹โ€‹โ€‹df_corr = df.corr(numeric_only=True) โ€‹โ€‹โ€‹โ€‹print(df_corr) โ€‹โ€‹โ€‹โ€‹ โ€‹โ€‹โ€‹โ€‹fig10 = plt.figure() โ€‹โ€‹โ€‹โ€‹sb.heatmap(df_corr, vmin=-1, vmax=1, annot=True, fmt='.1f') โ€‹โ€‹โ€‹โ€‹st.pyplot(fig10) โ€‹โ€‹โ€‹โ€‹ if __name__ == '__main__' : โ€‹โ€‹โ€‹โ€‹main()