๐Ÿ••

Visualization

notion imagenotion image

๋ชฉ์ฐจ

ย 

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 : ๋งˆ์ปค ํฌ๊ธฐ
ย 

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[-]
notion imagenotion image
x = [100, 200, 300] y = [1, 2, 3] # y ๋จผ์ € ์ž…๋ ฅ value = pd.Series([1, 2, 3], [100, 200, 300]) plt.plot(value)
Out[-]
notion imagenotion image
x = [100, 200, 300] y = [1, 2, 3] plt.plot(x, y, ':r') # red์ƒ‰ ์ ์„ 
Out[-]
notion imagenotion image
x = [100, 200, 300] y = [1, 2, 3] plt.plot(x, y, color='#ff0000', linewidth=10) # 16์ง„์ˆ˜ ์ƒ‰์ƒ ๊ฐ’ (RGB) , ์„ ๋‘๊ป˜
Out[-]
notion imagenotion image
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')
notion imagenotion image
# ์ด๋ฏธ์ง€ ์ €์žฅํ•˜๊ธฐ 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[-]
notion imagenotion image
# ๋‘๊ฐœ ๊ทธ๋ž˜ํ”„ ๋™์‹œ์— ๊ทธ๋ฆฌ๊ธฐ 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[-]
notion imagenotion image
ย 

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[-]
notion imagenotion image

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[-]
notion imagenotion image
ย 

histogram

  • ๋„์ˆ˜๋ถ„ํฌํ‘œ๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ๋‚˜ํƒ€๋‚ธ ๋ฐฉ๋ฒ•์ด๋‹ค.
  • ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„๋Š” ๊ณ„๊ธ‰ ์ฆ‰ ๊ฐ€๋กœ๋ฅผ ์ƒ๊ฐํ•˜์ง€ ์•Š๊ณ  ์„ธ๋กœ์˜ ๋†’์ด๋กœ๋งŒ ๋‚˜ํƒ€๋‚ธ๋‹ค. (์ถœ์ฒ˜ : ์œ„ํ‚ค๋ฐฑ๊ณผ)
  • ์—ฐ์†ํ˜• ์ž๋ฃŒ๋ฅผ ๊ณ„๊ธ‰์œผ๋กœ ๋‚˜๋ˆ„์–ด ๊ณ„๊ธ‰๋ณ„ ๋„์ˆ˜๋ฅผ ๋ง‰๋Œ€๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค.
x = [np.random.randint(1, 7) for i in range(100000)]
plt.hist(x, bins=11) plt.show()
Out[-]
notion imagenotion image

Pie Chart

labels = ['one', 'two', 'three'] size = [100, 20, 10] plt.pie(size, labels=labels) plt.show()
Out[-]
notion imagenotion image
labels = ['one', 'two', 'three'] size = [100, 20, 10] plt.pie(size, labels=labels, shadow=True) # ๊ทธ๋ฆผ์ž ๋„ฃ๊ธฐ plt.show()
Out[-]
notion imagenotion image
labels = ['one', 'two', 'three'] size = [50, 20, 30] plt.pie(size, labels = labels, shadow = True, autopct = '%1.2f%%') # ์ž๋™ ๋น„์œจ ์ถ”๊ฐ€ํ•˜๊ธฐ plt.show()
Out[-]
notion imagenotion image
ย 

Bar Chart

  • ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ์˜ ๋ถ„ํฌ๋ฅผ ํŒŒ์•…ํ•œ๋‹ค.
plt.bar(['one', 'two', 'three'], [10, 20, 30]) plt.show()
Out[-]
notion imagenotion image
plt.barh(['one', 'two', 'three'], [10, 20, 30]) # ๊ฐ€๋กœ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ plt.show()
Out[-]
notion imagenotion image

์„  ์ƒ‰์ƒ๊ณผ ์Šคํƒ€์ผ


  • 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๋ฅผ ์ด์šฉํ•ด ๊ทธ๋ž˜ํ”„๊ฐ€ ์›น์—์„œ ๋น ๋ฅด๊ฒŒ ๊ทธ๋ ค์ง„๋‹ค.

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[-]
notion imagenotion image
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[-]
notion imagenotion image
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[-]
notion imagenotion image

Scatter Plot

# ํ•œ๊ตญ ๊ธฐ๋Œ€ ์ˆ˜๋ช…๊ณผ GDP์˜ ์ƒ๊ด€๊ด€๊ณ„ korea_data = px.data.gapminder().query("country == 'Korea, Rep.'") fig = px.scatter(korea_data, x = 'gdpPercap', y = 'lifeExp') fig.show()
Out[-]
notion imagenotion image

Pie Chart

fig = px.pie(values = [20, 30, 50]) fig.show()
Out[-]
notion imagenotion image
labels = ['one', 'two', 'three'] values = [20, 30, 50] fig =go.Figure(data = [go.Pie(values = values, labels = labels)]) # ๋ฒ”๋ก€ ์ถ”๊ฐ€ํ•˜๊ธฐ fig.show()
Out[-]
notion imagenotion image

์ด๋ฏธ์ง€ ๋ถ„์„

  • ์ด๋ฏธ์ง€๋Š” ์ž‘์€ ์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ํ”ฝ์…€์„ ๋ชจ์•„์„œ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค.
  • ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋Š” (์„ธ๋กœํ”ฝ์…€์ˆ˜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[-]
notion imagenotion image
plt.imshow(jeju[::-1]) # ์ƒํ•˜๋กœ ๋’ค์ง‘๊ธฐ
Out[-]
notion imagenotion image
plt.imshow(jeju[:, ::-1]) # ์ขŒ์šฐ๋กœ ๋’ค์ง‘๊ธฐ
Out[-]
notion imagenotion image
plt.imshow(jeju[::-1, :])
Out[-]
notion imagenotion image
plt.imshow(jeju[550:800, :]) # ๋ถ€๋ถ„๋งŒ ์ž˜๋ผ๋‚ด๊ธฐ
Out[-]
notion imagenotion image
plt.imshow(jeju[550:800, 500:720])
Out[-]
notion imagenotion image
plt.imshow(jeju[::3, ::3]) # 3์นธ์”ฉ ๊ฑด๋„ˆ ๋›ฐ๊ธฐ
Out[-]
notion imagenotion image
plt.imshow(jeju[::6, ::6]) # 6์นธ์”ฉ ๊ฑด๋„ˆ ๋›ฐ๊ธฐ : ํ™”์งˆ์ด ๊นจ์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค
Out[-]
notion imagenotion image
plt.imshow(jeju[::10, ::10]) # 10์นธ์”ฉ ๊ฑด๋„ˆ ๋›ฐ๊ธฐ : ํ™”์งˆ์ด ๋งŽ์ด ๊นจ์ง
Out[-]
notion imagenotion image
miniJeju = jeju[::10, ::10]
# ์ƒ‰ ์ •๋ณด๋ฅผ ์ผ๋ ฌ๋กœ ๋‚˜์—ดํ•˜๊ณ  ์–ด๋””์— ๋งŽ์ด ๋ถ„ํฌ ๋˜์—ˆ๋Š” ์ง€๋ฅผ ํ™•์ธํ•œ๋‹ค. plt.hist(miniJeju.ravel(), 256, [0, 256]) plt.show()
Out[-]
notion imagenotion image
ย 
ย 
# ํŠน์ •ํ•œ ๊ฐ’(50)์„ ์ง€์ •ํ•˜๊ณ  ๊ทธ ์ดํ•˜๋Š” ์›๋ž˜์˜ ์ƒ‰์œผ๋กœ ๋‚˜์˜ค๊ณ  ๋‹ค๋ฅธ ๊ฐ’์€ ๊ฒ€์ •์ƒ‰(0)์œผ๋กœ ์ง€์ •ํ•˜์—ฌ # ์ด๋ฏธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. miniJeju_ = np.where(miniJeju < 50, miniJeju, 0) plt.imshow(miniJeju_)
Out[-]
notion imagenotion image
plt.imshow(jeju[:, :, 0]) # 0๋ฒˆ์งธ ์ƒ‰๋งŒ ์ถœ๋ ฅํ•˜๊ธฐ
Out[-]
notion imagenotion image
plt.imshow(jeju[:, :, 1]) # 1๋ฒˆ์งธ ์ƒ‰๋งŒ ์ถœ๋ ฅํ•˜๊ธฐ
Out[-]
notion imagenotion image
plt.imshow(jeju[:, :, 2]) # 2๋ฒˆ์งธ ์ƒ‰๋งŒ ์ถœ๋ ฅํ•˜๊ธฐ
Out[-]
notion imagenotion image
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[-]
notion imagenotion image
ย