DashBoard_streamlit / pandas / matplotlib / seaborn

(임포트 함수)

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

(기본 설정 - 한글 폰트)

plt.rcParams['font.family'] = 'Malgun Gothic'
plt.rcParams['axes.unicode_minus'] = False # 음수 지정 x
sns.set(font='Malgun Gothic',
        rc={'axes.unicode_minus' : False},
        style='darkgrid'
        )

(기본 설정 - 페이지)

st.set_page_config(page_title="Matplotlib & Seaborn 튜토리얼", layout="wide")
st.title("Matplotlib & Seaborn 튜토리얼") # 제목

image.png

(데이터셋 불러오기 - tips dataset 사용)

tips = sns.load_dataset('tips')

소제목_데이터 미리보기

st.subheader('데이터 미리보기')
# tip 데이터셋 출력
st.dataframe(tips.head())

image.png

기본 막대 그래프_matplotlib & seaborn

st.subheader("1. 기본 막대 그래프")
fig, ax = plt.subplots(figsize=(10,6)) # 객체 지향 방식으로 차트 작성

## <https://seaborn.pydata.org/>
sns.barplot(data = tips, x = 'day', y = 'total_bill', ax = ax)

## <https://matplotlib.org/stable/api/index>
ax.set_title('요일별 평균 지불 금액') # matplotlib
ax.set_xlabel('요일') # matplotlib
ax.set_ylabel('평균 지불 금액($)') # matplotlib

# plt.show() # jupyter, colab에서 출력할 때 사용
## 웹상에 띄울 때 == streamlit
st.pyplot(fig)

객체 지향으로 작성하는 이유

그래프를 그리는 목적 == 예쁘게 (보기 좋게) 출력하기 위해

이를 위해

ax (객체지향)으로 접근할 경우 세부적으로 (더 세세하게) 접근할 수 있다.