์ 14์ฅ ๐๋ฐ์ดํฐ ์๊ฐํ_matplotlib๐
๐งท Matplotlib
- ํ์ด์ฌ ๊ธฐ๋ฐ ๋ฐ์ดํฐ ๋ถ์ ํ๊ฒฝ์์ ๊ฐ์ฅ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ ์๊ฐํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ํ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ 'pyplot'์ ํตํด ์ฃผ์ ๊ธฐ๋ฅ์ ์ฌ์ฉ
- plotly, seaborn ๋ฑ๊ณผ ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ต๊ทผ ๊ฒฝ์๋ ฅ์ ๊ฐ์ถ๊ณ ์์
- pandas ๊ฐ์ฒด์ ๊ธฐ๋ณธ์ ์ธ ์๊ฐํ ๋๊ตฌ
- plot() ๋ฉ์๋๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์๊ฐํ
- ์ข
๋ฅ
- ๋ผ์ธ ํ๋กฏ(line plot)
- ๋ฐ ์ฐจํธ(bar chart)
- ํ์คํ ๊ทธ๋จ(histogram)
- ๋ฐ์ค ํ๋กฏ(box plot)
- ์ค์บํฐ ํ๋กฏ(scatter plot)
- ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฐ์ ธ์ค๊ธฐ
โ import matplotlib.pyplot as plt
โ cmd: pip install matplotlib
plt.plot(y)
- ๊ธฐ๋ณธ : ๋ผ์ธ ํ๋กฏ(line plot)
- ๋ฐ์ดํฐ๊ฐ ์๊ฐ, ์์ ๋ฑ์ ๋ฐ๋ผ ๋ณํํ๋ ํ๋ฆ ํ์
- x๊ฐ ์ฃผ์ด์ง์ง ์์ ๊ฒฝ์ฐ ์๋ ์์ฑ :x = range(len(y))
-๋ฐ์ดํฐ ์์ฑ : ()๊ฐ์ ์์ดํ
์ ๊ฐ์ง๋ ๋ฆฌ์คํธ
- plot(data) : ๊ธฐ๋ณธ์ ์ผ๋ก y์ถ์ ๊ฐ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋งค์นญ์ํด
- x -> len(data) ๋งํผ์ ์ธ๋ฑ์ค๋ฒํธ๋ฅผ ์์ฑ
- ๊ธฐ๋ณธ plot = line plot
โ
plt.plot([ ])
1. x tick(x์ถ ๊ฐ) ์ค์
- plt.plot(x,y) : x์ ๋ฃ์ด์ค ๋ฆฌ์คํธ์ y์ ๋ฃ์ด์ค ๋ฆฌ์คํธ์ ๊ธธ์ด๊ฐ ๊ฐ์์ผํจ
โ
plt.plot([ ],[ ])
2. ์ถ ์ด๋ฆ๊ณผ plot ์ ๋ชฉ ์ง์
- plot ์ ๋ชฉ : plt.title('title')
- x์ถ ์ด๋ฆ : plt.xlabel('x_name')
- y์ถ ์ด๋ฆ : plt.ylabel('y_name')
โ
plt.plot([ ],[ ])
โ
plt.title(' ')
โ
plt.xlabel(' ')
โ
plt.ylabel(' ')
3. ๋ ๊ฐ์ ์ ์ ๊ทธ๋ฆฌ๊ธฐ
๊ฐ๊ฐ์ ๋ผ์ธ ๊ทธ๋ํ๋ฅผ ์์ฐจ์ ์ผ๋ก plt.plot() ๋ช
๋ น์ด๋ก ์
๋ ฅ
- ๋ฒ๋ก(legend) ์ถ๊ฐ
plt.legend()
4. ์คํ์ผ ์ง์
- plt.plot([x], y, [fmt], **kwargs)
- fmt : '[color][marker][line]'
- ์์, ๋ง์ปค, ์ ๋ฑ์ ๋ํ ํฌ๋งท ์ต์ ์ค์
- fmt : '[color][marker][line]'
1) ๋ง์ปค ์ฐธ์กฐ ๋ํ๋จผํธ ์ฃผ์
https://matplotlib.org/3.1.3/api/markers_api.html
2) ์์ ์ฐธ์กฐ ๋ํ๋จผํธ ์ฃผ์
https://matplotlib.org/3.1.0/gallery/color/named_colors.html
3) ๋ผ์ธ ์คํ์ผ ์ฐธ์กฐ ๋ํ๋จผํธ ์ฃผ์
https://matplotlib.org/gallery/lines_bars_and_markers/line_styles_reference.html
4-1) ์คํ์ผ ๋ฌธ์์ด ์ถ๊ฐ : [fmt] => color + maker + line ์์๋๋ก ๋ฌธ์์ด ์์ฑ
์ด๋ ์์๋ฅผ ์๋ตํ๋ฉด ํด๋น ์๋ต๋ ์์๋ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ ์ฉ
'o' : ๋ผ์ธ ์์ด ํฐ ์ (์๋ชจ์) ์ผ๋ก ๋ฐ์ดํฐ ์์น ํ์
โ
plt.plot([ ],[ ], 'o')
4-2) o- : ๋ง์ปค๋ ๋๊ทธ๋ผ๋ฏธ, ๋ผ์ธ์ ์ค์
fmt[์์][๋ง์ปค][์ ์ข
๋ฅ]
โ
plt.plot([1,2,3,4],[2,6,10,7], 'o-')
4-3) o-- : ๋ง์ปค๋ ๋๊ทธ๋ผ๋ฏธ, ์ ์ ์ ์
โ
plt.plot([ ],[ ], 'o--')
4-4) ro: : r(๋นจ๊ฐ์),o(๋ง์ปค),:(์ ์ผ๋ก๋ ์ ์ )
โ plt.plot([ ],[ ], 'ro:')
โป ๊ธฐํ ์คํ์ผ ํ๋ผ๋ฏธํฐ ์ข
๋ฅ
- color : ์์(magenta)
- linestyle : -.
- marker : ^
- markerfacecolor : orange
- markersize : 20
plt.plot([ ], [ ],
color = ' ', linestyle=' ', marker=' ',
markeredgecolor=' ', markerfacecolor=' ', markersize=' ')
์์ ๋ํ๋จผํธ ์ฐธ๊ณ ํด์ ๋ค์ํ ์คํ์ผ ๊ตฌํ ๊ฐ๋ฅ
5. ์ถ ๋ฒ์ ์ง์
๐ matplotlib.pyplot.axis
- plt.axis([xmin, xmax, ymin, ymax])
- xmin : x์ถ ์ต์๊ฐ
- xmax : x์ถ ์ต๋๊ฐ
- ymin : y์ถ ์ต์๊ฐ
- ymax : y์ถ ์ต๋๊ฐ
๐ matplotplib.pyplot.xlim / ylim
- plt.xlim(min, max)
- plt.ylim(min, max)
๐ Bar chart (๋ง๋ ๊ทธ๋ํ)
- ์ฌ๋ฌ ๊ฐ์ ์๋์ ์ธ ํฌ๊ธฐ๋ฅผ ๋น๊ตํ๊ธฐ์ ์ ํฉํ ํํ๋ฒ์ด๋ฉฐ ์ํ ์ฐจํธ๋ณด๋ค ๋์ฑ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋จ
- plt.bar(x, y)
- x ๋ฐ์ดํฐ๊ฐ ์นดํ ๊ณ ๋ฆฌ ๊ฐ์ธ ๊ฒฝ์ฐ์๋ bar, barh ๋ก ๋ฐ ์ฐจํธ ์๊ฐํ ํํ
- ๊ฐ๋ก ๋ฐฉํฅ์ผ๋ก ๋ฐ ์ฐจํธ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด barh
- barh ์ฐจํธ ์ฃผ์์ : x์ถ์ด ์ผ๋ฐ์ ์ผ๋ก y์ถ์ ์์น๋ฅผ ์๋ฏธ
ex)
x = ['๊ฐ', '๋', '๋ค']
y = [2,10,5]
plt.bar(x,y)
plt.show()
๐ ๋ฐ๋ง๋ค ๋ถ์ผ ์ด๋ฆ์ ๋ฐ๊ฟ์ฃผ๊ณ ์ถ๋ค๋ฉด, xticks๋ฅผ ์๋๋ค.
- ๊ฐ๊ฐ์ bar๋ง๋ค ์์ ๋ณ๊ฒฝํ๊ธฐ
๐งท plt.bar() ๊ฐ์ฒด set_color()
โ
bar_list = plt.bar(x,y)
bar_list[0].set_color('r')
bar_list[1].set_color('g')
ex)