๋ชฉ์ฐจ
๋ชฉ์ฐจMatplotlibBasic AttributesLine Plotlabel legend Scatter PlothistogramPie ChartBar Chart์ ์์๊ณผ ์คํ์ผPlotlyLine PlotBar ChartScatter PlotPie Chart์ด๋ฏธ์ง ๋ถ์
ย
Matplotlib
- ๋ฐ์ดํฐ๋ฅผ Chart, Plot์ผ๋ก ์๊ฐํํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
- 2003๋ 0.1 ์ถ์, 17๋ ์ด๋ ํจํค์ง
- ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ ์๊ฐํ ํจํค์ง์ด๋ค.
- ๊ธฐ๋ณธ ๊ตฌ์ฑ : ๊ทธ๋ฆผ(figure), ์ถ(axes)
- % matplotlib inline : jupyter notebook ๋ด์์ output์ ๋ณด์ฌ์ค๋ค.
ย
Basic Attributes
- alpha : ํฌ๋ช ๋
- logy : Y์ถ Log scaling
- kind : line, bar, barh, kde
- subplots : ์ฌ๋ฌ๊ฐ์ plot ๊ทธ๋ฆฌ๊ธฐ
- legend : subplot์ ๋ฒ๋ก ์ง์
- xlim, ylim : X์ถ๊ณผ Y์ถ์ ๊ฒฝ๊ณ(axis๋ก ํ๋ฒ์ ๊ทธ๋ฆด ์ ์์)
- grid : ๊ทธ๋ฆฌ๋ ํํ(True, False)
- title : ๊ทธ๋ํ์ ์ ๋ชฉ ์ง์
- linewidth : ๋ผ์ธ ๋์ด
- color : ์
- linestyle : ์ค์ , ์ ์ , 1์ ์์ ๋ฑ
- marker : ๋ง์ปค ์ง์
- markerfacecolor : ๋ง์ปค ์
- markersize : ๋ง์ปค ํฌ๊ธฐ
- ๊ทธ ์ธ Attribute :ย https://matplotlib.org/3.1.1/tutorials/introductory/pyplot.html
ย
Line Plot
import numpy as np import pandas as pd import matplotlib.pyplot as plt
%matplotlib inline
x = [100, 200, 300] y = [1, 2, 3] plt.plot(x, y)
Out[-]
x = [100, 200, 300] y = [1, 2, 3] # y ๋จผ์ ์ ๋ ฅ value = pd.Series([1, 2, 3], [100, 200, 300]) plt.plot(value)
Out[-]
x = [100, 200, 300] y = [1, 2, 3] plt.plot(x, y, ':r') # red์ ์ ์
Out[-]
x = [100, 200, 300] y = [1, 2, 3] plt.plot(x, y, color='#ff0000', linewidth=10) # 16์ง์ ์์ ๊ฐ (RGB) , ์ ๋๊ป
Out[-]
x = [100, 200, 300] y = [1, 2, 3] plt.plot(x, y, color='#ff0000', linewidth=10) plt.title('hello world', fontsize=20) # ์ ๋ชฉ plt.xlabel('hello', fontsize=10) # x์ถ ์ด๋ฆ plt.ylabel(' world', fontsize=10) # y์ถ ์ด๋ฆ
Out[-]
Text(0, 0.5, ' world')
# ์ด๋ฏธ์ง ์ ์ฅํ๊ธฐ x = [100, 200, 300] y = [1, 2, 3] plt.plot(x, y, color='#ff0000', linewidth=10) plt.title('hello world', fontsize=20) plt.xlabel('hello', fontsize=10) plt.ylabel(' world', fontsize=10) plt.savefig('sample.png')
Out[-]
# ๋๊ฐ ๊ทธ๋ํ ๋์์ ๊ทธ๋ฆฌ๊ธฐ x = np.linspace(0, 10, 100) y = np.sin(x) y_ = np.cos(x) plt.plot(x, y) plt.plot(x, y_, '-o') # -o ๋ชจ์์ผ๋ก ๊ทธ๋ฆฌ๊ธฐ
Out[-]
ย
label
- ๋ฌด์จ ๋ฐ์ดํฐ์ธ์ง ํ์ํด์ฃผ๋ ๋ฐฉ๋ฒ
legend
- ์ขํ์ถ์ ๋ฒ๋ก๋ฅผ ์ถ๊ฐํด์ฃผ๋ ๋ฐฉ๋ฒ
- loc : legend ์์น
- 'best' 0
- 'upper right' 1
- 'upper left' 2
- 'lower left' 3
- 'lower right' 4
- 'right' 5
- 'center left' 6
- 'center right' 7
- 'lower center' 8
- 'upper center' 9
- 'center' 10
ย
x = np.linspace(0, 10, 100) y = np.sin(x) y_ = np.cos(x) # ๋ฒ๋ก ์ถ๊ฐํ๊ธฐ plt.plot(x, y, label='sin') plt.plot(x, y_, '-o', label='cos') # 4๋ฒ ์์น์ ๋ฒ๋ก ๋ฃ๊ธฐ plt.legend(loc=4)
Out[-]
Scatter Plot
- ๋ณ์๋ค์ ์๊ด๊ด๊ณ, ๋ฐ์ง ์์น๋ฅผ ๋ํ๋ธ๋ค.
x = np.linspace(0, 10, 20) y = x ** 2 plt.scatter(x, y, color='r', alpha=0.5) plt.show() # ์ฐ์ํฅ ๊ทธ๋ํ๋ก x,y ๋ ๋ณ์๋ ์์ ์๊ด๊ด๊ณ์์ ๋ํ๋ธ๋ค.
Out[-]
ย
histogram
- ๋์๋ถํฌํ๋ฅผ ๊ทธ๋ํ๋ก ๋ํ๋ธ ๋ฐฉ๋ฒ์ด๋ค.
- ๋ง๋๊ทธ๋ํ๋ ๊ณ๊ธ ์ฆ ๊ฐ๋ก๋ฅผ ์๊ฐํ์ง ์๊ณ ์ธ๋ก์ ๋์ด๋ก๋ง ๋ํ๋ธ๋ค. (์ถ์ฒ : ์ํค๋ฐฑ๊ณผ)
- ์ฐ์ํ ์๋ฃ๋ฅผ ๊ณ๊ธ์ผ๋ก ๋๋์ด ๊ณ๊ธ๋ณ ๋์๋ฅผ ๋ง๋๋ก ๋ํ๋ธ๋ค.
x = [np.random.randint(1, 7) for i in range(100000)]
plt.hist(x, bins=11) plt.show()
Out[-]
Pie Chart
labels = ['one', 'two', 'three'] size = [100, 20, 10] plt.pie(size, labels=labels) plt.show()
Out[-]
labels = ['one', 'two', 'three'] size = [100, 20, 10] plt.pie(size, labels=labels, shadow=True) # ๊ทธ๋ฆผ์ ๋ฃ๊ธฐ plt.show()
Out[-]
labels = ['one', 'two', 'three'] size = [50, 20, 30] plt.pie(size, labels = labels, shadow = True, autopct = '%1.2f%%') # ์๋ ๋น์จ ์ถ๊ฐํ๊ธฐ plt.show()
Out[-]
ย
Bar Chart
- ๋ฒ์ฃผํ ์๋ฃ์ ๋ถํฌ๋ฅผ ํ์ ํ๋ค.
plt.bar(['one', 'two', 'three'], [10, 20, 30]) plt.show()
Out[-]
plt.barh(['one', 'two', 'three'], [10, 20, 30]) # ๊ฐ๋ก๋ฐฉํฅ ๊ทธ๋ํ plt.show()
Out[-]
์ ์์๊ณผ ์คํ์ผ
- ๊ณต์๋ฌธ์ :ย https://matplotlib.org/3.2.1/api/pyplot_summary.html
- plt.plot(x, y, color='red') -> red, green, blue ๋ฑ ์์ ์ด๋ฆ
- plt.plot(x, y, color='r') -> r, g, b, y, m(์ํ), k(๊ฒ์ ) ๋ฑ ์์ ์ด๋ฆ
- plt.plot(x, y, color='0.2') -> ํ์์กฐ(0-1์ฌ์ด ๊ฐ)
- plt.plot(x, y, color='#ff0000') -> 16์ง์ ์์ ๊ฐ
- plt.plot(x, y, linestyle='solid') -> ์ค์ ('-')
- plt.plot(x, y, linestyle='dashed') -> ํ์ ('--')
- plt.plot(x, y, linestyle='dashdot') -> 1์ ์์ ('-.')
- plt.plot(x, y, linestyle='dotted') -> ์ ์ (':')
- plt.plot(x, y, '--r') -> ๋นจ๊ฐ์ ํ์
- '.' point marker
- 'o' circle marker(plt.plot(x, y_, '-o'))
- '^' triangle_up marker
- 's' square marker
- 'p' pentagon marker
- '*' star marker
- '+' plus marker
- 'x' x marker
ย
Plotly
- ์ธํฐ๋ํฐ๋ธํ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๊ธฐ์ ์ ํฉํ ํจํค์ง
- ์น ์๊ฐํ์ธ ์๋ฐ์คํฌ๋ฆฝํธ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ D3๋ฅผ ์ด์ฉํด ๊ทธ๋ํ๊ฐ ์น์์ ๋น ๋ฅด๊ฒ ๊ทธ๋ ค์ง๋ค.
- ๊ณต์ํํ์ด์ง(https://plotly.com/python/) ํํ ๋ฆฌ์ผ
Line Plot
import plotly.express as px x_ = np.array([1, 2, 3, 4, 5]) y_ = x_ ** 2 fig = px.line(x=x_, y=y_) fig.show()
Out[-]
import plotly.express as px import plotly.graph_objects as go # gapminder ๋ฐ์ดํฐ ์ ์ฌ์ฉํ๊ธฐ korea_life = px.data.gapminder().query("country == 'Korea, Rep.'") # ํ๊ตญ์ธ ๊ธฐ๋ ์๋ช fig = px.line(korea_life, x="year", y="lifeExp", title='Life expectancy in Korea') fig.show()
Out[-]
korea_life["year"] korea_life["lifeExp"]
Out[-] 840 47.453 841 52.681 842 55.292 843 57.716 844 62.612 845 64.766 846 67.123 847 69.810 848 72.244 849 74.647 850 77.045 851 78.623 Name: lifeExp, dtype: float64
ย
Bar Chart
# ํ๊ตญ GDP ๋ฐ์ดํฐ ์ korea_gdp = px.data.gapminder().query("country == 'Korea, Rep.'") fig = px.bar(korea_gdp, x='year', y='gdpPercap', title = "ํ๊ตญ์ธ GDP") fig.show()
Out[-]
Scatter Plot
# ํ๊ตญ ๊ธฐ๋ ์๋ช ๊ณผ GDP์ ์๊ด๊ด๊ณ korea_data = px.data.gapminder().query("country == 'Korea, Rep.'") fig = px.scatter(korea_data, x = 'gdpPercap', y = 'lifeExp') fig.show()
Out[-]
Pie Chart
fig = px.pie(values = [20, 30, 50]) fig.show()
Out[-]
labels = ['one', 'two', 'three'] values = [20, 30, 50] fig =go.Figure(data = [go.Pie(values = values, labels = labels)]) # ๋ฒ๋ก ์ถ๊ฐํ๊ธฐ fig.show()
Out[-]
์ด๋ฏธ์ง ๋ถ์
- ์ด๋ฏธ์ง๋ ์์ ์ฌ๊ฐํ ๋ชจ์์ ํฝ์ ์ ๋ชจ์์ ๊ตฌ์ฑ๋์ด์๋ค.
- ์ด๋ฏธ์ง ํฌ๊ธฐ๋ (์ธ๋กํฝ์ ์X๊ฐ๋กํฝ์ ์)๋ก ํํํ๋ค.
- ์ด๋ฏธ์ง๋ฅผ ์ ์ฅํ ๋๋ ์์ ํํํ๋ ์ค์นผ๋ผ ๊ฐ์ด๋ 2์ฐจ์ vector๋ก ํํํ๋ค.
- RGB๋ ์๊ณต๊ฐ์ ๋งํ๋ค. Red, Green, Blue๋ก ์ธ๊ฐ๊ฐ ํฉ์ณ์ง ๋ฒกํฐ์ด๋ค.
ย
- skimage(scikit-image) : ์ด๋ฏธ์ง ์ฒ๋ฆฌ๋ฅผ ์ํ ํ์ด์ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค. numpy array๋ก ๋์ํ๋ค.
ย
- PIL(Python Image Library)
- Pillow : ์ด๋ฏธ์ง ์ฒ๋ฆฌ์ ๊ทธ๋ํฝ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ํ์ด์ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
(๋์ด์ PIL์ ์ง์ํ์ง ์๊ณ ๊ทธ ํ์ ํ๋ก์ ํธ์ธ Pillow๋ฅผ ์ฌ์ฉํ๋ค)
ย
- cv2 : OpenCV๋ Open Source Computer Vision Library์ ์ฝ์๋ก ์คํ์์ค ์ปดํจํฐ ๋น์ ๋ฐ ๋จธ์ ๋ฌ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค. ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ถ๋ฌ์ฌ ๋๋ cv2๋ฅผ ์ฌ์ฉํ๋ค.
ย
import numpy as np from skimage import io # from PIL import Image # from cv2 import matplotlib.pyplot as plt
jeju = io.imread('jeju.jpg') # ์ด๋ฏธ์ง ๋ถ๋ฌ์ค๊ธฐ
type(jeju) # ์ด๋ฏธ์ง ํ์ ํ์ธํ๊ธฐ
Out[-] imageio.core.util.Array
jeju.shape
Out[-] (960, 1280, 3) # ์ธ๋ก, ๊ฐ๋ก, RGB(์)
jeju
Out[-] Array([[[171, 222, 251], [172, 223, 252], [172, 223, 252], ..., [124, 189, 255], [121, 189, 254], [120, 188, 253]], [[173, 224, 253], [173, 224, 253], [173, 224, 253], ..., [124, 189, 255], [122, 190, 255], [121, 189, 254]], [[174, 225, 254], [174, 225, 254], [175, 226, 255] ..., [125, 190, 255], [122, 190, 255], [122, 190, 255]], ..., [[ 66, 93, 26], [ 89, 114, 46], [ 49, 72, 2], ..., [ 2, 29, 0], [ 34, 59, 17], [ 40, 63, 21]], [[ 44, 71, 4], [ 23, 50, 0], [ 29, 52, 0], ..., [ 40, 67, 22], [ 0, 19, 0], [ 16, 41, 0]], [[ 29, 58, 0], [ 44, 71, 2], [ 84, 110, 37], ..., [ 17, 44, 1], [ 33, 60, 17], [ 18, 43, 1]]], dtype=uint8)
np.min(jeju), np.max(jeju)
Out[-] (0, 255)
plt.imshow(jeju)
Out[-]
plt.imshow(jeju[::-1]) # ์ํ๋ก ๋ค์ง๊ธฐ
Out[-]
plt.imshow(jeju[:, ::-1]) # ์ข์ฐ๋ก ๋ค์ง๊ธฐ
Out[-]
plt.imshow(jeju[::-1, :])
Out[-]
plt.imshow(jeju[550:800, :]) # ๋ถ๋ถ๋ง ์๋ผ๋ด๊ธฐ
Out[-]
plt.imshow(jeju[550:800, 500:720])
Out[-]
plt.imshow(jeju[::3, ::3]) # 3์นธ์ฉ ๊ฑด๋ ๋ฐ๊ธฐ
Out[-]
plt.imshow(jeju[::6, ::6]) # 6์นธ์ฉ ๊ฑด๋ ๋ฐ๊ธฐ : ํ์ง์ด ๊นจ์ง ๊ฒ์ ๋ณผ ์ ์๋ค
Out[-]
plt.imshow(jeju[::10, ::10]) # 10์นธ์ฉ ๊ฑด๋ ๋ฐ๊ธฐ : ํ์ง์ด ๋ง์ด ๊นจ์ง
Out[-]
miniJeju = jeju[::10, ::10]
# ์ ์ ๋ณด๋ฅผ ์ผ๋ ฌ๋ก ๋์ดํ๊ณ ์ด๋์ ๋ง์ด ๋ถํฌ ๋์๋ ์ง๋ฅผ ํ์ธํ๋ค. plt.hist(miniJeju.ravel(), 256, [0, 256]) plt.show()
Out[-]
ย
ย
# ํน์ ํ ๊ฐ(50)์ ์ง์ ํ๊ณ ๊ทธ ์ดํ๋ ์๋์ ์์ผ๋ก ๋์ค๊ณ ๋ค๋ฅธ ๊ฐ์ ๊ฒ์ ์(0)์ผ๋ก ์ง์ ํ์ฌ # ์ด๋ฏธ์ง๋ฅผ ์ถ๋ ฅํ๋ค. miniJeju_ = np.where(miniJeju < 50, miniJeju, 0) plt.imshow(miniJeju_)
Out[-]
plt.imshow(jeju[:, :, 0]) # 0๋ฒ์งธ ์๋ง ์ถ๋ ฅํ๊ธฐ
Out[-]
plt.imshow(jeju[:, :, 1]) # 1๋ฒ์งธ ์๋ง ์ถ๋ ฅํ๊ธฐ
Out[-]
plt.imshow(jeju[:, :, 2]) # 2๋ฒ์งธ ์๋ง ์ถ๋ ฅํ๊ธฐ
Out[-]
jeju[:, :, 0] # 0๋ฒ์งธ ์ ์ ๋ณด ์ถ๋ ฅ
Out[-] Array([[171, 172, 172, ..., 124, 121, 120], [173, 173, 173, ..., 124, 122, 121], [174, 174, 175, ..., 125, 122, 122], ..., [ 66, 89, 49, ..., 2, 34, 40], [ 44, 23, 29, ..., 40, 0, 16], [ 29, 44, 84, ..., 17, 33, 18]], dtype=uint8)
jeju[:, :]
Out[-] Array([[[171, 222, 251], [172, 223, 252], [172, 223, 252], ..., [124, 189, 255], [121, 189, 254], [120, 188, 253]], [[173, 224, 253], [173, 224, 253], [173, 224, 253], ..., [124, 189, 255], [122, 190, 255], [121, 189, 254]], [[174, 225, 254], [174, 225, 254], [175, 226, 255], ..., [125, 190, 255], [122, 190, 255], [122, 190, 255]], ..., [[ 66, 93, 26], [ 89, 114, 46], [ 49, 72, 2], ..., [ 2, 29, 0], [ 34, 59, 17], [ 40, 63, 21]], [[ 44, 71, 4], [ 23, 50, 0], [ 29, 52, 0], ..., [ 40, 67, 22], [ 0, 19, 0], [ 16, 41, 0]], [[ 29, 58, 0], [ 44, 71, 2], [ 84, 110, 37], ..., [ 17, 44, 1], [ 33, 60, 17], [ 18, 43, 1]]], dtype=uint8)
# ํ์์ผ๋ก ์ด๋ฏธ์ง ์ถ๋ ฅํ๊ธฐ from skimage import color plt.imshow(color.rgb2gray(jeju), cmap=plt.cm.gray)
Out[-]
ย