๋ชฉ์ฐจ
Matplotlib: ํ์ด์ฌ์ ๋ฐ์ดํฐ ์๊ฐํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
Matplotlib์ ํ์ด์ฌ์์ ๊ฐ์ฅ ๋ํ์ ์ผ๋ก ์ฌ์ฉ๋๋ ๋ฐ์ดํฐ ์๊ฐํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํจํค์ง์ด๋ค. ์ด๋ฅผ ์ฌ์ฉํ๋ฉด Line Plot, Bar, chart, Pie chart, Histogram, Box Plot ๋ฑ ๋ค์ํ ์ฐจํธ์ ์คํ์ผ์ ์ฝ๊ฒ ๊ตฌํํ ์ ์๋ค.
์ฝ๋์์๋ ๋ณดํต `import matplotlib.pyplot as plt` ๋ฅผ ์ ์ธํ์ฌ ์ฌ์ฉํ๋ค. ๊ทธ๋ฆฌ๊ณ ์ดํ์๋ `plt`๋ฅผ ํตํด ํจ์๋ฅผ ํธ์ถํ๋ค. ์ด๋ ๊ฒ ์ฌ์ฉํ๋ ์ด์ ๋ `matplotlib.pyplot`์ ๋ฐ๋ณต์ ์ธ ํธ์ถ์ ํผํ๊ธฐ ์ํด์์ด๋ค. ๋ฐ๋ผ์ `plt`๋ฅผ ์ฌ์ฉํ๋ฉด ์ฝ๋๊ฐ ๊ฐ๊ฒฐํด์ง๊ณ ์๊ฐํ ์์ ์ด ํธ๋ฆฌํด์ง๋ค.
Matplotlib ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น - ๋ก์ปฌ ๐ป
๋ฐ์ดํฐ ์๊ฐํ๋ฅผ ํจ๊ณผ์ ์ผ๋ก ํ๊ธฐ ์ํด ํด๋น ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ๋ค.
pip install matplotlib
Matplotlib ์ฌ์ฉํ๊ธฐ - ์ฝ๋ฉ ๐
์ฝ๋ฉ์์๋ ์ด๋ฏธ ์ค์น๊ฐ ๋์ด ์๊ธฐ ๋๋ฌธ์ ํด๋ผ์ฐ๋ ์ค์น๊ฐ ํ์์๋ค.
ํด๋น ์ฝ๋๋ ์ฌ๊ณ์ ๋ณ ์ฝ๋ก๋ ํ์ง์์๋ฅผ ์๊ฐํํ์ฌ ๊ทธ๋ํ๋ก ๋ํ๋ธ ๊ฒ์ด๋ค. ๊ฐ ์์ฆ๋ณ ํ์ง์ ์๊ฐ ์ด๋ป๊ฒ ๋ณํ๋์ง ํ๋์ ํ์ ํ ์ ์๋ค.
import matplotlib.pylab as plt
season = ['spring', 'summer', 'autumn', 'winter']
corona = [400, 800, 600, 1200]
#plt.plot(season,corona)
plt.bar(season,corona, width=0.5, color="orange")
plt.title("Corona status by season")
plt.xlabel("season")
plt.ylabel("num of infectee")
plt.show()
์ฝ๋ ์ค๋ช
- `import matplotlib.pylab ad plt` : Matplotlib ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ถ๋ฌ์์ plt๋ก ๋ณ์นญ ์ง์ . ์ด๋ฅผ ํตํด Matplotlib์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋ค.
- `season = []` : ์์ฆ ์ ๋ณด๋ฅผ ๋ํ๋ด๋ ๋ฆฌ์คํธ๋ฅผ ์์ฑํ๋ค. x์ถ์ผ๋ก ์ฌ์ฉ๋๋ค.
- `corona=[]` : ์์ฆ๋ณ ์ฝ๋ก๋ ํ์ง์ ์๋ฅผ ๋ํ๋ด๋ ๋ฆฌ์คํธ๋ฅผ ์์ฑํ๋ค. y ์ถ์ผ๋ก ์ฌ์ฉ๋๋ค.
- `plt.plot()` : plot ํจ์๋ฅผ ์ฌ์ฉํ์ฌ x์ถ์๋ ์์ฆ, y์ถ์๋ ์ฝ๋ก๋ ํ์ง์ ์๋ฅผ ์ค์ ํ์ฌ ์ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฐ๋ค.
๋ฐ์ดํฐ ์๊ฐํ ๊ณ์ ์ ๋ฐ์ดํฐ๋ฅผ ๋ํ๋ด๋ ๊ทธ๋ํ
์ ์ ๊ฐ์ด๋ ์ฌ์ฉ๋ฒ
์๋ ๋งํฌ ์ ์ ๊ฐ์ด๋์ ์ฌ์ฉ๋ฐฉ๋ฒ์ด ์์ธํ ๋์์๋ค ์ฐธ๊ณ !
๊ฐ๊ฐ์ ์ฐจํธ๋ฅผ ๋ฐ๋ก๋ณด๊ธฐ ๐
Matplotlib์ plt. Matplotlib(A,B,C) ํจ์๋ ์ฌ๋ฌ ๊ฐ์ ์ฐจํธ๋ฅผ ํ๋์ ํ๋ฉด์ ๋ฐฐ์ด ํํ๋ก ์ถ๋ ฅํ ๋ ์ฌ์ฉ๋๋ค. ์ดํจ์๋ ์ธ๊ฐ์ ์ธ์๋ฅผ ๋ฐ๋๋ค.
plt.subplot(A,B,C)
- A: ์ ์ฒด ๊ทธ๋ฆผ ์์ญ์ ํ์ผ๋ก ๋๋ ๊ฐ์
- B: ์ ์ฒด ๊ทธ๋ฆผ ์์ญ์ ์ด๋ก ๋๋ ๊ฐ์
- C: ํ์ฌ ๊ทธ๋ฆด ์ฐจํธ์ ์์น๋ฅผ ๋ํ๋ธ๋ค.
์๋ฅผ ๋ค์ด `plt.subplot(2,2,1)`์ ์ ์ฒด ๊ทธ๋ฆผ์์ญ์ 2ํ 2์ด๋ก ๋๋๋ค, ํ์ฌ ๊ทธ๋ฆด ์ฐจํธ์ ์๋ฅผ ์ฒซ๋ฒ์งธ๋ก ์ค์ ํ๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค. ์ด๋ฌํ ํํ๋ก ์ฌ์ฉํ๋ฉด ํ ํ๋ฉด์ ์ฌ๋ฌ ๊ฐ์ ์๋ธ ์ฐจํธ๋ฅผ ๋ฐฐ์ด ํํ๋ก ์ถ๋ ฅํ ์ ์๋ค. ์๊ฐํ๋ฅผ ์ถ๋ ฅํ ๋ ๊ฐ๊ฐ์ ์ฐจํธ๋ฅผ ๋ค๋ฅธ ๋ชจ์์ผ๋ก ํ ์ ์๋ค.
import matplotlib.pylab as plt
season = ['spring', 'summer', 'autumn', 'winter']
corona = [400, 800, 600, 1200]
plt.subplot(2,1,1)
plt.plot(season,corona)
plt.subplot(2,1,2)
plt.bar(season,corona, width=0.5, color="orange")
plt.title("Corona status by season")
plt.xlabel("season")
plt.ylabel("num of infectee")
plt.show()
์ฝ๋๋ก ์ถ๋ ฅ๋ ๊ทธ๋ํ
์ฐ๋๋ณ๋ก ์ ๊ทธ๋ํ๋ก ์๊ฐํ ๐
์ ์ฝ๋๋ ์ถ๊ตฌ ์ ์๋ค์ ๋ ์ดํ ์ ์ฐ๋๋ณ๋ก ์ ๊ทธ๋ํ๋ก ์๊ฐํํ๋ ์์์ด๋ค.
import matplotlib.pyplot as plt
son = [6.53, 7.14, 6.99, 7.1, 6.15] #์ํฅ๋ฏผ ์ฐ๋๋ณ ๋ ์ดํ
kane = [7.53,7.68, 7.6, 7.38,7.4] #ํด๋ฆฌ์ผ์ธ ์ฐ๋๋ณ ๋ ์ดํ
erikson = [7.4, 7.53, 7.4, 7.08, 6.51] #์๋ฆญ์ผ์ ์ฐ๋๋ณ ๋ ์ดํ
year = [2015, 2016, 2017, 2018, 2019 ]
plt.plot(year, erikson, 'g--', label='erikson')
plt.plot(year, son, 'b-', label='son-heung-min')
plt.plot(year, kane, 'r:', label='harry kane')
plt.xlim(2014.5, 2019.5)
plt.ylim(6,8)
plt.legend(loc =8)
plt.xlabel("year")
plt.ylabel("rating")
plt.show()
- ` plt.plot(year, erikson, 'g--', label='erikson') ` : ๊ฐ ์ ์์ ์ฐ๋๋ณ ๋ ์ดํ ์ ์ ๊ทธ๋ํ๋ก ํํํ๋ค. g--๋ ์ด๋ก์ ์ ์ , b๋ ํ๋์ ์ค์ , r:์ ๋นจ๊ฐ์ ์ ์ ์ ์๋ฏธํ๋ค. lable์ ํตํด ๊ฐ ์ ์์ ์ด๋ฆ์ ํ์ํ๋ค.
- ` plt.xlim(2014.5, 2019.5) ` : x์ถ์ ๋ฒ์๋ฅผ ์ค์ ํ๋ค.
- ` plt.legend(loc =8) ` : ๋ฒ๋ก๋ฅผ ํ์ํ๋ค. loc๋ฒ๋ก์ ์์น๋ฅผ ์ง์ ํ๋๋ฐ 8์ ํ๋จ ์ค์์ ์๋ฏธํ๋ค.
์ถ๊ตฌ์ ์๋ค์ ์ฐ๋๋ณ ๋ ์ดํ
์ด๋ฅผ ํตํด ์ํฅ๋ฏผ, ํด๋ฆฌ์ผ์ธ, ์๋ฆญ์ผ์ ์ฐ๋๋ณ ๋ ์ดํ ์ ํ๋์ ๋ณผ์ ์๋ค.
์ฐ์ ๋ ๋ถํฌ ํ์ธํ๊ธฐ ๐
์ ์ฝ๋๋ ์ถ๊ตฌ ์ ์๋ค์ ๊ณจ๊ณผ ์ด์์คํธ๋ฅผ ์ฐ์ ๋(์ค์บํฐ ํ๋กฏ)๋ก ์๊ฐํํ๋ ์์์ด๋ค.
import matplotlib.pyplot as plt
goals = [13, 9, 8,4, 3] #๊ฐ ์ ์์ ๊ณจ์
assists = [2,9,4,2,0] #๊ฐ ์ ์์ ์ด์์คํธ ์
players = ["kane", "Son","Alli", "erikson", "Moura"]
plt.scatter(goals, assists) #x์ถ๊ณผy์ถ์ผ๋ก ์ค์ ํ์ฌ ์ฐ์ ๋๋ฅผ ๊ทธ๋ฆฐ๋ค.
for goal, assist, player in zip(goals, assists, players):
plt.annotate(player,
xy = (goal, assist),
xytext = (5,-5),
textcoords = "offset points"
)
plt.title("players goal and assists")
plt.xlim(0,15)
plt.xlabel("Goals")
plt.ylabel("Assists")
plt.show()
- ` for goal, assist, player in zip(goals, assists, players) ` : ์ธ ๊ฐ์ ๋ฆฌ์คํธ(goals, assists, players)์์ ๊ฐ๊ฐ์ ์์๋ฅผ ํ ๋ฒ์ ํ๋์ฉ ๊ฐ์ ธ์ค๋ ๊ฒ์ ์๋ฏธ
- `zip()` : ํจ์๋ ์ฌ๋ฌ ๊ฐ์ ๋ฆฌ์คํธ๋ฅผ ๋์์ ๋ฌถ์ด์ฃผ๋ ์ญํ ์ํ๋ค. ์๋ฅผ ๋ค์ด, zip([1, 2, 3], ['a', 'b', 'c'])๋ [(1, 'a'), (2, 'b'), (3, 'c')]์ ๊ฐ์ด ๊ฐ ๋ฆฌ์คํธ์ ๊ฐ์ ์ธ๋ฑ์ค์ ์๋ ์์๋ค์ ํํ ํํ๋ก ๋ฌถ์ด์ค๋ค.
- goals, assists, players ๋ฆฌ์คํธ์ ๊ฐ์ ์ธ๋ฑ์ค์ ์๋ ์์๋ค์ ํ ๋ฒ์ ๊ฐ์ ธ์์ goal, assist, player ๋ณ์์ ์์๋๋ก ํ ๋นํ๋ค.
- `plt.annotate()`: ๊ทธ๋ํ ์์ ์ฃผ์์ ์ถ๊ฐํ๋ ํจ์์ด๋ค. ์ด ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ ์ ์์ ์ด๋ฆ์ ํด๋น ๊ณจ๊ณผ ์ด์์คํธ์ ์์น์ ํ์ํ๋ค.
-
- textcoords="offset points": ์ฃผ์ ํ ์คํธ์ ์์น๋ฅผ ํฝ์ ๋จ์๋ก ์ง์ ํจ์ ๋ํ๋ธ๋ค.
-