๐ ๋ฐ์ดํฐ ์๊ฐํ – CSV ๋ณํฉ ๋ฐ ์ด์ค Y์ถ ๊ทธ๋ํ (macOS + Python)
โ ๋ฌธ์ ์ค๋ช
์ฌ๋ฌ ๊ฐ์ CSV ํ์ผ์ ๋ณํฉํ์ฌ DataFrame์ผ๋ก ๊ตฌ์ฑํ ๋ค, ๋ณต์ ๋ฐ์ดํฐ๋ฅผ ํ๋์ ์๊ฐํ์ ํํํ ์ ์๋๋ก ์ด์ค y์ถ์ ํ์ฉํ ์๊ฐํ ๊ตฌํ
๐ ์ ํ ์กฐ๊ฑด
• CSV ํ์ผ ์ง์ ๋ณํฉ
• ์ธ์ฝ๋ฉ์ ๊ธฐ๋ณธ UTF-8 ์ฌ์ฉ
• matplotlib ์๊ฐํ ์ฌ์ฉ
• macOS ํ๊ฒฝ ํ๊ธ ํฐํธ ์ ์ฉ
• ๋ ๊ฐ์ง ์๊ฐํ (ํธํก๊ธฐ ์งํ์ ์ + ๋ฏธ์ธ๋จผ์ง, ์ฌ๊ณ ๊ฑด์ + ๊ฐ์๋)
๐ ํ์ด ์ ๋ต
• csv.reader๋ก ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
• ๋ณํฉ๋ ๋ด์ฉ์ ์๋ก์ด CSV๋ก ์ ์ฅ
• pandas๋ก ์ฝ์ด๋ค์ฌ ์ด์ค y์ถ ๊ทธ๋ํ ๊ตฌ์ฑ
• macOS์ ๊ธฐ๋ณธ ํ๊ธ ํฐํธ(AppleGothic) ์ค์
• ์๊ฐ์ ์ผ๋ก ์ง๊ด์ ์ธ ๋น๊ต๋ฅผ ์ํ bar + line ์กฐํฉ ๊ตฌ์ฑ
โ ์ฝ๋ฉ (macOS ๊ธฐ์ค Python)
import csv
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
# ํ์ผ ์ฝ๊ธฐ
with open('์ฌ๊ณ .csv') as f1:
datas = list(csv.reader(f1))
with open('๊ฐ์๋.csv') as f2:
datas2 = list(csv.reader(f2))
with open('ํธํก๊ธฐ์งํ.csv') as f3:
datas3 = list(csv.reader(f3))
with open('PM10.csv', encoding='utf-8') as f4:
datas4 = list(csv.reader(f4))
with open('PM2.5.csv', encoding='utf-8') as f5:
datas5 = list(csv.reader(f5))
# ์ฒซ ๋ฒ์งธ ๋ฐ์ดํฐ ๋ณํฉ
with open('output_.csv', 'w', encoding='utf-8') as out1:
cnt = 0
for i in range(len(datas3)):
if i == 0:
out1.write(datas3[i][0] + ", " + datas3[i][1] + ", PM10, PM2.5\n")
elif datas3[i][0] != '':
cnt += 1
out1.write(datas3[i][0] + ", " + "".join(datas3[i][1].split(",")) + ", " + datas4[cnt][1] + ", " + datas5[cnt][1] + "\n")
# macOS ํ๊ธ ํฐํธ ์ค์
mpl.rc('font', family='AppleGothic')
# ๊ทธ๋ํ 1: ํธํก๊ธฐ ์งํ์ ์ + ๋ฏธ์ธ๋จผ์ง
df = pd.read_csv('output_.csv', index_col='์ง๋ฃ๋
์')
fig, ax1 = plt.subplots(figsize=(12, 4))
ax1.set_title('์๋ณ ํธํก๊ธฐ์งํ์ ์์ ๋๊ธฐ์ค์ผ์ ๊ด๊ณ')
ax1.set_xlabel('์ง๋ฃ๋
์')
ax1.set_ylabel('ํ์ ์', color='tab:blue')
ax1.bar(df.index, df[' ํ์ ์'], color='tab:blue', alpha=0.6)
ax1.tick_params(axis='y', labelcolor='tab:blue')
plt.xticks(rotation=90)
ax2 = ax1.twinx()
ax2.set_ylabel('PM10/PM2.5', color='tab:green')
ax2.plot(df.index, df[' PM10'], color='tab:green', marker='o')
ax2.plot(df.index, df[' PM2.5'], color='tab:red', marker='o')
ax2.tick_params(axis='y', labelcolor='tab:green')
fig.legend(loc='upper right')
plt.tight_layout()
plt.show()
# ๋ ๋ฒ์งธ ๋ฐ์ดํฐ ๋ณํฉ
with open('output.csv', 'w', encoding='utf-8') as out2:
for i in range(len(datas)):
out2.write(datas[i][0] + ", " + datas[i][1] + ", " + datas2[i][0] + "\n")
# ๊ทธ๋ํ 2: ์ฌ๊ณ ๋ฐ์ ๊ฑด์ + ๊ฐ์๋
df = pd.read_csv('output.csv', index_col='์์ ')
fig, ax1 = plt.subplots(figsize=(12, 4))
ax1.set_title('์๋ณ ๊ฐ์๋๊ณผ ์ฐจ๋์ฌ๊ณ ๋ฐ์ ๊ฑด์')
ax1.set_xlabel('์์ ')
ax1.set_ylabel('๋ฐ์๊ฑด์ (๊ฑด)', color='tab:blue')
ax1.bar(df.index, df[' ๋ฐ์๊ฑด์ (๊ฑด)'], color='tab:blue', alpha=0.6)
ax1.tick_params(axis='y', labelcolor='tab:blue')
plt.xticks(rotation=90)
ax2 = ax1.twinx()
ax2.set_ylabel('๊ฐ์๋(mm)', color='tab:green')
ax2.plot(df.index, df[' ๊ฐ์๋(mm)'], color='tab:green', marker='o')
ax2.tick_params(axis='y', labelcolor='tab:green')
fig.legend(loc='upper right')
plt.tight_layout()
plt.show()
๐ก ํด์ค
• AppleGothic ํฐํธ๋ macOS ๊ธฐ๋ณธ ํ๊ธ ํฐํธ๋ก matplotlib ํ๊ธ ๊นจ์ง ๋ฌธ์ ํด๊ฒฐ
• twinx()๋ฅผ ํ์ฉํ ์ด์ค y์ถ ํํ์ผ๋ก ๋ ๋ฐ์ดํฐ์ ์๊ด๊ด๊ณ ์๊ฐํ
• ํ๋์ ์ฐจํธ ๋ด์์ bar์ line์ ํจ๊ป ํ์ฉํ์ฌ ๋ณ์ ๊ฐ ํ๋ฆ ๋น๊ต ๊ฐ๋ฅ
• ๋ฐ์ดํฐ ๋ถ์ ์ด๊ธฐ ๋จ๊ณ์์ ๊ด๊ณ์ฑ ์๊ฐํ์ ํจ๊ณผ์