
๋ฐ์ดํฐ ์๊ฐํ๋ ๋ฐฉ๋ํ ์์ ๋ฐ์ดํฐ๋ฅผ ์ฐจํธ, ๊ทธ๋ํ, ์ง๋ ๋ฑ ์๊ฐ์ ์ธ ์์๋ก ๋ณํํ์ฌ ํํํ๋ ๊ฒ์ ๋๋ค. ์ธ๊ฐ์ ๋๋ ํ ์คํธ๋ณด๋ค ์ด๋ฏธ์ง๋ฅผ ํจ์ฌ ๋น ๋ฅด๊ฒ ์ฒ๋ฆฌํ๋ฏ๋ก, ๋ณต์กํ ๋ฐ์ดํฐ ์์์ ํจํด, ์ถ์ธ, ์ด์์งํ๋ฅผ ํ์ ํ๋ ๋ฐ ํ์์ ์ ๋๋ค.
๋ฐ์ดํฐ์ ๋ชฉ์ ์ ๋ฐ๋ผ ์ ์ ํ ์ฐจํธ๋ฅผ ์ ํํด์ผ ํฉ๋๋ค.
| ๋ถ์ ๋ชฉ์ | ์ถ์ฒ ์ฐจํธ | ์ค๋ช |
|---|---|---|
| ๋น๊ต (Comparison) | ๋ง๋ ์ฐจํธ (Bar Chart) | ํญ๋ชฉ ๊ฐ์ ํฌ๊ธฐ๋ฅผ ๋น๊ตํ ๋ ๊ฐ์ฅ ๋ณดํธ์ ์ ๋๋ค. |
| ์ถ์ธ (Trend) | ๋ผ์ธ ์ฐจํธ (Line Chart) | ์๊ฐ์ ํ๋ฆ์ ๋ฐ๋ฅธ ๋ฐ์ดํฐ์ ๋ณํ๋ฅผ ๋ณด์ฌ์ค๋๋ค. |
| ๋น์ค (Composition) | ํ์ด ์ฐจํธ (Pie Chart) | ์ ์ฒด์์ ๊ฐ ๋ถ๋ถ์ด ์ฐจ์งํ๋ ๋น์จ์ ๋ณด์ฌ์ค๋๋ค. |
| ๊ด๊ณ (Relationship) | ์ฐ์ ๋ (Scatter Plot) | ๋ ๋ณ์ ๊ฐ์ ์๊ด๊ด๊ณ๋ฅผ ํ์ ํ ๋ ์ฌ์ฉํฉ๋๋ค. |
| ๋ถํฌ (Distribution) | ํ์คํ ๊ทธ๋จ (Histogram), Box Plot | ๋ฐ์ดํฐ์ ๋น๋ ๋ถํฌ๋ฅผ ํ์ธํ ๋ ์ฌ์ฉํฉ๋๋ค. |
| ์ง๋ฆฌ ์ ๋ณด | ๋งต (Map) | ์์น ๋ฐ์ดํฐ๋ฅผ ์ง๋ ์์ ์๊ฐํํฉ๋๋ค. |
NumPy์ Pandas์ ํจ๊ป ๋ฐ์ดํฐ ๊ณผํ ๋ถ์ผ์์ ํ์์ ์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค.matplotlib.pyplot์ Matplotlib์ ๋ง์น MATLAB์ฒ๋ผ ์๋ํ๊ฒ ํ๋ ํจ์๋ค์ ๋ชจ์์
๋๋ค.plt๋ผ๋ ๋ณ์นญ(alias)์ผ๋ก import ํ์ฌ ์ฌ์ฉํฉ๋๋ค.import matplotlib.pyplot as plt
import numpy as np
plot)๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฝ๋๋ค.
matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)
# x,y: array-like, float.
# fmt: str, <color><marker><line> ์์ ์๊ด ์์. kwargs๋ก ๋์ฒด ๊ฐ๋ฅ
# scalex, scaley: bool
# data: indexable object. ์ฃผ์ด์ง๋ ๊ฒฝ์ฐ, x,y์ label name์ด ์์ผ ํจ.

x = [1, 2, 3, 4]
y = [10, 20, 30, 40]
plt.plot(x, y)
plt.show()
scatter)x์ y์ ์๊ด๊ด๊ณ๋ฅผ ์ ์ผ๋ก ํํํฉ๋๋ค.
matplotlib.pyplot.scatter(x, y, s=None, c=None, *, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=None, colorizer=None, plotnonfinite=False, data=None, **kwargs)
# x, y: float, array-like (shape (n,))
# s: float, array-like. ์ ๊ณฑ๊ฐ
# c: array-like, list of color, color
# marker: MarkerStyle, Default 'o'
# cmap: str, Colormap
# norm: str, Normalize
# alpha: float[0, 1]. Transparency

plt.scatter(x, y)
plt.show()
bar)๋ฒ์ฃผํ ๋ฐ์ดํฐ์ ๊ฐ์ ๋ง๋๋ก ํํํฉ๋๋ค.
matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
# x: float, array-like
# height: float, array-like
# width: float, array-like
# bottom: float, array-like. Default 0.
# align: 'center', 'edge'. edge๋ bar์ ์ผ์ชฝ์ ๋ง์ถค. ์ค๋ฅธ์ชฝ์ ๋ง์ถ๋ ค๋ฉด width๋ฅผ ์์๋ก.

categories = ['A', 'B', 'C']
values = [100, 200, 150]
plt.bar(categories, values)
plt.show()
plt.title("Graph Title")
plt.xlabel("X-axis Label")
plt.ylabel("Y-axis Label")linestyle), ์์(color), ๋ง์ปค(marker) ๋ฑ์ ์ง์ ํ ์ ์์ต๋๋ค.plt.legend()๋ฅผ ํตํด ๋ฐ์ดํฐ์ ๋ผ๋ฒจ์ ํ์ํฉ๋๋ค.plt.grid(True)๋ก ๊ฒฉ์๋ฅผ ํ์ํ ์ ์์ต๋๋ค.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2, 100) # 0๋ถํฐ 2๊น์ง 100๊ฐ์ ๊ตฌ๊ฐ์ผ๋ก ๋๋ ๋ฐ์ดํฐ ์์ฑ
plt.figure(figsize=(5, 2.7)) # ๊ทธ๋ํ ํฌ๊ธฐ ์ค์ (์ธ์น ๋จ์)
plt.plot(x, x, label='linear') # ์ ํ ๊ทธ๋ํ
plt.plot(x, x**2, label='quadratic') # 2์ฐจ ํจ์ ๊ทธ๋ํ
plt.xlabel('x label') # x์ถ ๋ผ๋ฒจ
plt.ylabel('y label') # y์ถ ๋ผ๋ฒจ
plt.title("Simple Plot") # ๊ทธ๋ํ ์ ๋ชฉ
plt.legend() # ๋ฒ๋ก ํ์
plt.show() # ๊ทธ๋ํ ์ถ๋ ฅ
ํ๋์ ํ๋ฉด(Figure) ์์ ์ฌ๋ฌ ๊ฐ์ ๊ทธ๋ํ(Axes)๋ฅผ ๋ฐฐ์นํ ์ ์์ต๋๋ค.

# x ๋ฐ์ดํฐ ์์ฑ : 0๋ถํฐ 10๊น์ง 100๊ฐ์ ์
x = np.linspace(0, 10, 100)
# 2ํ 1์ด์ ๊ตฌ์กฐ ์ค ์ฒซ ๋ฒ์งธ ๊ทธ๋ํ, ์ถ(frame) ์์ ๊ธฐ
# OO-style๋ก Axes(ax1, ax2)๋ฅผ ์์ฑ
# OO-style: Figure์ Axes๋ฅผ ๋ช
์์ ์ผ๋ก ์์ฑํ์ฌ method๋ฅผ ํธ์ถ
ax1 = plt.subplot(2, 1, 1, frameon=False)
plt.plot(x, x, label='linear') # ์ ํ ๊ทธ๋ํ
# 2ํ 1์ด์ ๊ตฌ์กฐ ์ค ๋ ๋ฒ์งธ ๊ทธ๋ํ, ์ถ(frame) ๋ณด์ด๊ธฐ
ax2 = plt.subplot(2, 1, 2, frameon=True)
plt.plot(x, [i**2 for i in x], label='quadratic') # 2์ฐจ ํจ์ ๊ทธ๋ํ
plt.show()
Matplotlib์ ๊ธฐ๋ฐ์ผ๋ก ๋ ์ธ๋ จ๋ ์คํ์ผ๊ณผ ํต๊ณ์ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๊ณ ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค. Pandas DataFrame๊ณผ ํธํ์ฑ์ด ๋งค์ฐ ์ข์ต๋๋ค.
relplot (๊ด๊ณ), displot (๋ถํฌ), catplot (๋ฒ์ฃผํ) ๋ฑ ๋ชฉ์ ์ ๋ง๋ ํจ์ ์ ๊ณต.ํ ๋ฐ์ดํฐ์ ์ ์ฌ์ฉํ์ฌ ์ ์ฒด ๊ธ์ก๊ณผ ํ ์ฌ์ด์ ๊ด๊ณ๋ฅผ ์๊ฐํํฉ๋๋ค.

import seaborn as sns
# ๊ธฐ๋ณธ ํ
๋ง ์ ์ฉ
sns.set_theme()
# ์์ ๋ฐ์ดํฐ์
๋ก๋ (ํ ๋ฐ์ดํฐ)
tips = sns.load_dataset("tips")
# ๊ด๊ณํ ๊ทธ๋ํ ์์ฑ (Relational Plot)
sns.relplot(
data=tips,
x="total_bill", y="tip", # x์ถ: ์ด ๊ธ์ก, y์ถ: ํ
col="time", # ์๊ฐ๋(์ ์ฌ/์ ๋
)๋ณ๋ก ์ปฌ๋ผ ๋ถ๋ฆฌ
hue="smoker", # ํก์ฐ ์ฌ๋ถ์ ๋ฐ๋ผ ์์ ๋ค๋ฅด๊ฒ
style="smoker", # ํก์ฐ ์ฌ๋ถ์ ๋ฐ๋ผ ๋ง์ปค ๋ชจ์ ๋ค๋ฅด๊ฒ
size="size", # ์ธ์์์ ๋ฐ๋ผ ์ ํฌ๊ธฐ ๋ค๋ฅด๊ฒ
)
Box Plot๊ณผ KDE(๋ฐ๋) ๊ทธ๋ํ๋ฅผ ํฉ์น ํํ๋ก, ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ํ๋ถํ๊ฒ ๋ณด์ฌ์ค๋๋ค.

# ์์ผ(day)๋ณ ์ ์ฒด ๊ธ์ก(total_bill) ๋ถํฌ๋ฅผ ๋ฐ์ด์ฌ๋ฆฐ ํํ๋ก ์๊ฐํ
sns.violinplot(
data=tips,
x="day", y="total_bill",
hue="smoker", # ํก์ฐ ์ฌ๋ถ๋ก ์ชผ๊ฐ์ ๋น๊ต
split=True, # ๋ฐ์ด์ฌ๋ฆฐ์ ๋ฐ์ผ๋ก ๊ฐ๋ผ ์์ชฝ์ ํ์
inner="quart", # ๋ด๋ถ์ ์ฌ๋ถ์์ ํ์
)
relplot, displot, catplot (์ฌ๋ฌ ๊ทธ๋ํ๋ฅผ ํ ๋ฒ์ ๊ด๋ฆฌ)scatterplot, lineplot, histplot, barplot, boxplot ๋ฑ (๊ฐ๋ณ ๊ทธ๋ํ)Python ๋ฐ์ดํฐ๋ฅผ Leaflet.js ๊ธฐ๋ฐ์ ์ธํฐ๋ํฐ๋ธ ์ง๋๋ก ์๊ฐํํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค.

import folium
# ์ง๋ ์์ฑ (์๋, ๊ฒฝ๋ ์ค์ฌ)
m = folium.Map(location=[37.5665, 126.9780], zoom_start=16)
# ๋ง์ปค ์ถ๊ฐ
folium.Marker(
[37.5665, 126.9780],
popup='Seoul City Hall', # ํด๋ฆญ ์ ๋์ค๋ ํ์
tooltip='Click me!' # ๋ง์ฐ์ค ์ค๋ฒ ์ ๋์ค๋ ํดํ
).add_to(m)
# ์ง๋ ์ถ๋ ฅ (Jupyter Notebook ํ๊ฒฝ)
m
CircleMarker: ์ค ๋ ๋ฒจ๊ณผ ์๊ด์์ด ํ๋ฉด์ ํฌ๊ธฐ(Pixel)๊ฐ ๊ณ ์ ๋จ.Circle: ์ง๋์์ ์ค์ ๋ฐ๊ฒฝ(Meter)์ ๊ฐ์ง (์ค์ธํ๋ฉด ์ปค์ง).
import folium
m = folium.Map(location=[37.5105, 127.0150], zoom_start=14)
# ํ๋ฉด์ ํฌ๊ธฐ ๊ณ ์ (50 ํฝ์
)
folium.CircleMarker(
location=[37.5, 127.0],
radius=50,
color="blue",
fill=True
).add_to(m)
# ์ค์ ์ง๋ฆฌ์ ๋ฐ๊ฒฝ ๊ณ ์ (1000 ๋ฏธํฐ = 1km)
folium.Circle(
location=[37.52, 127.02],
radius=1000,
color="red",
fill=True
).add_to(m)
m
์ง์ญ๋ณ ํต๊ณ์น(์: ์ธ๊ตฌ ๋ฐ๋, ์ค์ ๋ฅ )๋ฅผ ์์์ผ๋ก ํํํ๋ ์ง๋์ ๋๋ค. GeoJSON ๋ฐ์ดํฐ(์ง๋ ๊ฒฝ๊ณ)์ ํต๊ณ ๋ฐ์ดํฐ(CSV ๋ฑ)์ ์ฌ์ฉํฉ๋๋ค.

import json
import requests
import folium
df = pd.read_csv('data/code_bas.csv')
r = requests.get('https://raw.githubusercontent.com/southkorea/seoul-maps/master/kostat/2013/json/seoul_municipalities_geo_simple.json')
c = r.content
seoul_geo = json.loads(c)
m = folium.Map(
location=[37.559819, 126.963895],
zoom_start=11,
)
folium.GeoJson(
seoul_geo,
name='์ง์ญ๊ตฌ'
).add_to(m)
m
๋ฐ์ดํฐ ์๊ฐํ ์ด๋ก
๋๊ตฌ ์ฌ์ฉ๋ฒ