2์ฃผ์ฐจ ๊ธ์์ผ
- matplotlib
- seaborn
๋ฐ์ดํฐ๋ฅผ ๋ค์ํ ๋ฐฉ์์ผ๋ก ์๊ฐํํ ์ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ. pandas
์ ์ฐ๋ ๊ฐ๋ฅํ๋ค. ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๊ธฐ ์ํด์ pyplot
์ด๋ผ๋ ์๋ธ๋ชจ๋์ ์ฌ์ฉํ๋ค. pyplot
๋ชจ๋์ ์๋ฌต์ ์ผ๋ก plt
๋ผ๋ ์ฝ์นญ(alias)์ผ๋ก ํธ์ถํ๋ค.
matplotlib document ์์ ์์ธํ ์ค๋ช
๊ณผ ์์ ๋ฅผ ์ฐพ์๋ณผ ์ ์๋ค.
plt.plot()
'''================ ๋ฐ๋๋ผ๋ง ๊ทธ๋ํ ================'''
import numpy as np
import matplotlib.pyplot as plt
import math
x = np.arange(-10,10,0.5)
y = np.array([i**2 + 2*i + 5 for i in x])
plt.plot(x,y) # ๊ทธ๋ํ์ ๋ ์ถ์ ์ค์
plt.show() # plot์ ํ๋ฉด์ ๋์ด๋ค
plot ์ต์ ์ ๋ฐ๋ก ์ง์ ํ์ง ์์ ๊ฒฝ์ฐ ๋ง์ปค์ ์ ์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์์์ ์ค์ ๋๋ค. ์ต์ ์ ์ฝ์์ผ๋ก ์ค์ ํ ์๋ ์๊ณ , ๊ธธ๊ฒ ํ์ด์ ์ธ ์๋ ์๋ค.
'''========== line ์ต์
์ ์ฝ์์ผ๋ก ์ค์ =========='''
x = np.arange(-10,10,0.5)
y = np.array([i**2 + 2*i + 5 for i in x])
plt.plot(x,y,'-ro') # -ro : ์ ์คํ์ผ - , ์ ์ red , ๋ง์ปค ๋ชจ์ o
plt.show()
'''========== ํค์๋๋ฅผ ์ฌ์ฉํด line ์ต์
์ค์ =========='''
x = np.arange(-10,10,0.5)
y = np.array([i**2 + 2*i + 5 for i in x])
plt.plot(x,y,linewidth=3,linestyle='--' ,marker='*',
markersize=10, color='#8ed130')
# keyword argument๋ฅผ ์ฌ์ฉํด์ ์ข๋ ์์ธํ ์ค์ ํ ์ ์๋ค.
plt.show()
ํ plot ์์ ๋ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆด ์๋ ์๋ค.
'''========== ํ plot ์์ 2๊ฐ์ ๊ทธ๋ํ ๊ทธ๋ฆฌ๊ธฐ =========='''
x = np.arange(0,10,0.05)
sin_x = np.array([math.sin(i) for i in x])
cos_x = np.array([math.cos(i) for i in x])
plt.plot(x,sin_x) # plot์ 2๊ฐ ์์ ๋๊ณ
plt.plot(x,cos_x)
plt.show() # plt.show()๊ฐ ํธ์ถ๋๋ฉด ํ๋ฒ์ plush ํ๋ค
ํ ํ๋ฉด ์์ ์ฌ๋ฌ ๊ฐ์ subplot์ ๋ฐฐ์นํ ์๋ ์๋ค.
'''========== subplot ์ฌ๋ฌ ๊ฐ ๋ฐฐ์นํ๊ธฐ =========='''
x = np.arange(0,10,0.05)
sin_x = np.array([math.sin(i) for i in x])
cos_x = np.array([math.cos(i) for i in x])
plt.subplot(2,1,1) # ๊ทธ๋ํ๋ฅผ 2ํ 1์ด๋ก ๋ฐฐ์น, 1๋ฒ์งธ ๊ทธ๋ํ
plt.plot(x, sin_x, '--r')
plt.subplot(2,1,2) # 2ํ 1์ด๋ก ๋ฐฐ์น, 2๋ฒ์งธ ๊ทธ๋ํ
plt.plot(x, cos_x, '--b')
plt.show()
๊ทธ๋ํ ์ ๋ชฉ๊ณผ ์ถ์ ๋ ์ด๋ธ์ด๋ ๋ฒ์ฃผ๋ฅผ ๋ฌ ์๋ ์๋ค.
'''========== Title, Label, Legend =========='''
x = np.arange(0,10,0.05)
sin_x = np.array([math.sin(i) for i in x])
cos_x = np.array([math.cos(i) for i in x])
plt.plot(x,sin_x)
plt.plot(x,cos_x)
plt.xlabel('time')
plt.ylabel('signal')
plt.title('time/signal graph', fontsize = 15)
plt.legend(['signal 1 - sin', 'signal 2 - cos'], loc='upper right')
plt.show()
plt.bar()
'''========== ๊ธฐ๋ณธ์ ์ธ ๋ง๋ ๊ทธ๋ํ =========='''
index = np.arange(7)
# range ์จ๋ ๋๋๋ฐ ๊ทธ๋ํ ์ฌ๋ฌ ๊ฐ ๊ฒน์น ๋๋
# np.arange() ๋ฅผ ์จ์ผ ์ธ๋ฑ์ค ์์น+๊ทธ๋ํ ํญ ์ฐ์ฐ์ด ๋๋ค
label = ['mon','tue','wed','thr','fri','sat','sun']
data = [5,6,2,7,8,4,1]
plt.bar(index, data, color='gold')
plt.title('visitors',fontsize=12)
plt.xlabel('day',fontsize=12)
plt.ylabel('people',fontsize=12)
plt.xticks(index,label,fontsize=12) # index์ label ๋งค์น
plt.show()
์ฌ๋ฌ ๊ทธ๋ํ๋ฅผ ๊ฐ๋ก๋ก ๋๋ํ ๋๊ณ ๊ทธ๋ฆด ์๋ ์๋ค.
'''========== ๋ ๊ทธ๋ํ ๋๋ํ ๋๊ธฐ =========='''
case_right = [84,76,94,97]
case_wrong = [16,24,6,3]
label = ['cat','dog','bird','frog']
index = np.arange(4)
wid = 0.35
plt.bar(index, case_wrong, wid, color='#e36052')
plt.bar(index+0.35, case_right, wid, color='#66a62b') # ๋๊ฐ ๋๋ํ
plt.title('Prediction result',fontsize=12)
plt.xlabel('category')
plt.ylabel('case')
plt.legend(['Incorrect','Correct'])
# legend๋ ๋ฐ๋ก ์์น ์ต์
์ ์ค์ ํ์ง ์์ผ๋ฉด ์์์ ์๋ฆฌ๋ฅผ ์ก์์ค๋ค
plt.xticks(index,label)
plt.show()
์ฌ๋ฌ ๊ทธ๋ํ๋ฅผ ์ธ๋ก๋ก ์์์ ๊ทธ๋ฆด ์๋ ์๋ค.
'''========== ์ธ๋ก๋ก ๊ทธ๋ํ ์๊ธฐ =========='''
case_right = [84,76,94,97]
case_wrong = [16,24,6,3]
# right/wrong ์ผ์ด์ค์ ํฉ์ด ์ผ์ ํด์ ์ด๋ ๊ฒ ์์ผ๋ฉด ์ด์๋ค
label = ['cat','dog','bird','frog']
index = range(4)
wid = 0.5
plt.bar(index, case_wrong, wid, color='#e36052')
plt.bar(index, case_right, wid, color='#66a62b', bottom=case_wrong)
# case_wrong์ ์๋ซ๋ถ๋ถ์ด ์ด case_right ๊ทธ๋ํ์ ์์์
plt.title('Prediction result',fontsize=12)
plt.xlabel('category')
plt.ylabel('case',)
plt.legend(['Incorrect','Correct'])
plt.xticks(index,label)
plt.ylim(0,120) # ๊ทธ๋ํ์์ ๊ฐ์ ํ์ํ ์์ญ์ ํ๊ณ ์ค์
plt.show()
matplotlib
๋ก๋ ๋ค์ํ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆด ์ ์์ง๋ง, tplotlib
์ wrapper์ธ seaborn
์ ์ด์ฉํ๋ฉด ๋ ์์ ๊ทธ๋ํ๋ฅผ ์ฝ๊ฒ ๊ทธ๋ฆด ์ ์๋ค. searbon
๋ชจ๋์ ์๋ฌต์ ์ผ๋ก sns
๋ผ๋ ์ฝ์นญ(alias)์ผ๋ก ํธ์ถํ๋ค.
seaborn document ์์ ์์ธํ ์ค๋ช
๊ณผ ์์ ๋ฅผ ์ฐพ์๋ณผ ์ ์๋ค.
์ค๋ ๊ฐ์๊ฐ ์ด๋ ค์์ ์ด๋ฐ์๋ ๊ฐ์ ํ์ฐธ ๊ฐ์๋ฅผ ๋ค์ฌ๋ค๋ณด๊ณ ์์๋ค. ์ด๋ ์ ๋ ๋ฃ๊ณ ๋์ ์ด์ ์ฒ๋ผ ๊ฐ์์๋ฃ๋ฅผ ๊ฐ์ด ๋ณด๋ฉด์ ์ด์ผ๊ธฐ๋ฅผ ๋๋ด๋ค.
๊ณต์ ํผ์ด์ธ์
์์ ์๊ฐ์ธ 2์์ ๋ชจ์ด๋ฉด ๊ฐ์๋ฅผ ์ถฉ๋ถํ ์ํํ์ง ๋ชปํ๊ณ ๋ชจ์ด๊ฒ ๋๋ ๊ฒ ๊ฐ์ ๋ค์ ์ฃผ์๋ ๊ฐ์๋ฅผ ์ข ๋ ๋ฃ๊ณ 4์๋ถํฐ ์์ํ๊ธฐ๋ก ํ๋ค.