๊ทธ๋ํ ํ๊ธํํ๊ธฐ
matplotlib ์์ ํ๊ธ์ ๋ฃ๊ณ ์ถ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์ ๋ ฅํ๋ฉด ๋ฉ๋๋ค
#๊ทธ๋ํ ํ๊ธํํ๊ธฐ
import platform
import matplotlib.pyplot as plt
from matplotlib import font_manager, rc
plt.rcParams['axes.unicode_minus'] = False
if platform.system() == 'Linux':
rc('font', family='NanumGothic')
def run_compare() :
#๊ทธ๋ํ ํ๊ธํํ๊ธฐ
plt.rcParams['axes.unicode_minus'] = False
if platform.system() == 'Linux':
rc('font', family='NanumGothic')
elif platform.system() == 'Windows':
font_path = "c:\Windows\Fonts\BATANG.TTC" # ํ๊ธ ํฐํธ ํ์ผ ๊ฒฝ๋ก ์ง์ ํ๊ธฐ
font_name = font_manager.FontProperties(fname=font_path).get_name()
rc('font', family=font_name)
๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
์ฌ์ฉํ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ต๋๋ค.
๋ฐ์ดํฐ๋ ์ฃผํผํฐ๋ ธํธ๋ถ์์ 2024/4/29์๋ก ๊ตญ๋ด ETF ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์์ต๋๋ค
์ฌ์ฉํ ๋ฐ์ดํฐ๋ ์๋์ ๊ฐ์ต๋๋ค
col1, col2, col3 = st.columns([1, 2.5, 1]) # ํ๋ฉด์ ๊ฐ์ด๋ฐ ์ ๋ ฌํ๊ธฐ ์ํจ์ด๋ค
with col2 :
# 1. ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
etf_list = pd.read_csv('./KRETF.csv')
# 2. ์ ์ฒด ์ค๋ช
ํ๊ธฐ
st.title('โ
ข. ๊ตญ๋ด ETF ์ฃผ์๊ฐ์น๋น๊ต')
st.header(' : Comparing domestic ETF stock valuations')
st.text('ETF๋ฐ์ดํฐ๋ 2024/4/29 ๊ธฐ์ค ๋ฐ์ดํฐ์
๋๋ค.')
st.text('ETF๋ ํ๋์ ์ฃผ์์ผ๋ก ๊ฑฐ๋๋์ง๋ง ์ฌ๋ฌ ์์ฐ์ ๋ํ ์์ ๊ถ์ ๊ฐ์ง ์ ์๊ฒ ํด์ค๋๋ค.')
st.text('์ฝ๊ฒ ๋งํ๋ฉด ์ฌ๋ฌ ์ฑ๊ถ๊ณผ ์ฃผ์, ์์ฐ์ ๋ด๊ณ ์๋ ํ๋์ ์์๋ก ํํ๋ฉ๋๋ค.')
st.text('์ฃผ์์ ๋ถ์ฐํฌ์ํ๋ ๊ฒฝ์ฐ, risk๊ฐ ์๋ก ์์๋์ด ์์ ์ฑ์ด ๋์์ง๊ธฐ ๋๋ฌธ์')
st.text('๊ทธ ์์ฒด๋ก ๋ถ์ฐํฌ์๊ฐ ๊ฐ๋ฅํ ETF๋ ์๊ธ์์ค์ํ์ด ์ ์ ์์ ํ ํฌ์๋์์ผ๋ก ํ๊ฐ๋ฉ๋๋ค')
st.header('')
์นดํ ๊ณ ๋ฆฌ๋ณ ์ต์ , ์ต๋ ์ด์์จ ๋ฐ์ดํฐํ๋ ์ ์ถ๋ ฅํ๊ธฐ st.datafarame()
# 5. ์นดํ
๊ณ ๋ฆฌ๋ณ ์ต์ , ์ต๋ ์ด์์จ
st.header('')
st.header('')
st.subheader('Category๋ณ ์ต์ , ์ต๋ ์ด์์จ')
st.header('')
st.text('์ต์ , ์ต๋ ์ด์์จ์ ์ ์ ์์ต๋๋ค. ์ด์์จ์ด ๋ง์ด๋์ค์ธ ๊ฒฝ์ฐ, ์๊ธ๋ณด์ฅ์ด ์ด๋ ต์ต๋๋ค')
# 5-(1) max() ์ฌ์ฉํ์ฌ ์ต๋ ์ด์์จ ๋ฐ์ดํฐํ๋ ์ ์ถ๋ ฅ
etf_list3 = etf_list.groupby('Category')['EarningRate'].max().reset_index()
etf_list3['Category_Name'] = etf_list2['Category_Name']
etf_list3 = etf_list3.sort_values('EarningRate',ascending=False)
etf_list3 = etf_list3[['Category','Category_Name','EarningRate']]
st.dataframe(etf_list3)
# 5-(2) min() ์ฌ์ฉํ์ฌ ์ต์ ์ด์์จ ๋ฐ์ดํฐํ๋ ์ ์ถ๋ ฅ
etf_list4 = etf_list.groupby('Category')['EarningRate'].min().reset_index()
etf_list4['Category_Name'] = etf_list2['Category_Name']
etf_list4 = etf_list4.sort_values('EarningRate')
etf_list4 = etf_list4[['Category','Category_Name','EarningRate']]
st.dataframe(etf_list4)
# 5-(3) ์ต๋, ์ต์ ์ด์์จ ๊ทธ๋ํ ์ถ๋ ฅ
fig1 = plt.figure(figsize=(6,6))
plt.grid(True)
plt.bar(data=etf_list ,x='Category',height='EarningRate', width=0.6, color='pink')
plt.axhline(y=0, color='black') # y์ถ ์งํ๊ฒ
plt.xticks([2,4,5,3,1,7,6],['๊ตญ๋ด์
์ข
/ํ
๋ง ETF','ํด์ธ์ฃผ์','์์์ฌ','๊ตญ๋ดํ์','๊ตญ๋ด์์ฅ์ง์ ETF','๊ธฐํ','์ฑ๊ถ'],rotation=45)
plt.title('์นดํ
๊ณ ๋ฆฌ๋ณ ์ต์ ,์ต๋ ์ด์์จ ๊ทธ๋ํ')
plt.ylabel('์ด์์จ')
st.pyplot(fig1)
# 6. Category๋ณ ์ด์์จ ํ๊ท ๊ทธ๋ํ ํ์ํ๊ธฐ
st.header('')
st.header('')
st.subheader('Category๋ณ EarningRate์ ํ๊ท ')
st.header('')
# 6-(1) Category๋ณ ์ด์์จ ํ๊ท ์ ๋ฐ์ดํฐํ๋ ์ ์ถ๋ ฅ
etf_list2.sort_values(by='EarningRate',ascending=False, inplace=True)
st.dataframe(etf_list2)
st.text('์นดํ
๊ณ ๋ฆฌ๋ณ ์ด์์จ ํ๊ท ์ ๋์ ์์๋๋ก ์ ๋ ฌํ์์ต๋๋ค')
# 6-(2) Category๋ณ ์ด์์จ ํ๊ท ์ ๊ทธ๋ํ ์ถ๋ ฅ
fig = plt.figure(figsize=(6,6))
X = [1, 3, 5, 7, 9, 11, 13] # X ๊ฐ๊ฒฉ ์ค์
plt.title('์นดํ
๊ณ ๋ฆฌ๋ณ ์ด์์จ ํ๊ท ๊ทธ๋ํ')
plt.bar(X, etf_list2['EarningRate'], width=1.25, color='orange')
plt.axhline(y=0, color='black') # y์ถ ์งํ๊ฒ
plt.grid(True)
plt.ylabel('์ด์์จ ํ๊ท ')
ticklabel = ['๊ตญ๋ด์์ฅ์ง์ ETF', '๊ตญ๋ด์
์ข
/ํ
๋ง ETF', 'ํด์ธ์ฃผ์', '์์์ฌ', '๊ธฐํ', '๊ตญ๋ดํ์', '์ฑ๊ถ'] # ๋ผ๋ฒจ 1234567์์ ๋ฐ๊พธ๊ธฐ
plt.xticks(X, ticklabel, rotation=45)
st.pyplot(fig)