PART 1: Data Visualization Techniques
import matplotlib.pyplot as plt
import numpy as np
단순 Plot
fig, ax = plt.subplots() # plot() 보다 세부적으로 plot 조정 가능
ax.plot([1, 2, 3, 4], [1, 4, 5, 7])
x = np.linspace(0, 2, 100) # 0 ~ 2 사이는 균등하게 100개로 쪼개라
# Objected Oriented Style
fig, ax = plt.subplots()
ax.plot(x, x, label = "linear")
ax.plot(x, x**2, label = "quadratic")
ax.plot(x, x**3, label = "cubic")
ax.set_title("Simple plot")
ax.set_xlabel("x axis")
ax.set_ylabel("y axis")
ax.legend() # 범주 박스 추가
plt.show()
Plot Style
# 임의 데이터
data1, data2, data3, data4 = np.random.randn(4, 100)
data1.shape, data2.shape, data3.shape, data4.shape
((100,), (100,), (100,), (100,))
Pyplot Style (Plot 배치 형태)
fig, (ax1, ax2) = plt.subplots(1, 2) # Plot 배치 유형
ax1.plot(data1, data2)
ax1.set_title("first plot")
ax2.plot(data3, data4)
ax2.set_title("second plot")
plt.show()
fig, (ax1, ax2) = plt.subplots(2, 1, figsize = (6, 10))
ax1.plot(data1, data2)
ax1.set_title("first plot")
ax2.plot(data3, data4)
ax2.set_title("second plot")
plt.show()
Object Oriented Style (Plot 모양)
# 임의 데이터
x = np.arange(100)
x, type(x)
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]),
numpy.ndarray)
fig, ax = plt.subplots(figsize = (6, 3))
ax.plot(x, np.cumsum(x), c = "#891414", alpha = 0.2, lw = 5, ls = "--")
plt.show()
fig, ax = plt.subplots(figsize = (6, 3))
ax.plot(x, np.cumsum(x), c = "#891414", alpha = 0.2, lw = 5, ls = ":")
plt.show()
fig, ax = plt.subplots(figsize = (6, 3))
ax.plot(x, np.cumsum(x), c = "#891414", alpha = 0.2, lw = 5, ls = "-.")
plt.show()
fig, ax = plt.subplots(figsize = (10, 8))
ax.scatter(data1, data2)
plt.show()
plt.rcParams["figure.dpi"] = 200
fig, ax = plt.subplots()
ax.scatter(data1, data2, marker = "^", label = "First figure")
ax.scatter(data3, data4, marker = "3", label = "Second figure")
ax.legend()
plt.show()
# plt.rcParams["lines.linestyle"] = "--"
plt.rc("lines", linewidth=2, linestyle="--")
data = np.random.randn(50)
plt.plot(data)
plt.show()
폰트 지정
# 1. 윈도우 폰트 저장위치에서 폰트 불러오기
import matplotlib.font_manager
font_list = matplotlib.font_manager.findSystemFonts(fontpaths = None, fontext = "ttf")
font_list
['C:\\Windows\\Fonts\\PALSCRI.TTF',
'C:\\WINDOWS\\Fonts\\trebucbi.ttf',
'C:\\WINDOWS\\Fonts\\ERASDEMI.TTF',
'C:\\Windows\\Fonts\\BOOKOSBI.TTF',
'C:\\WINDOWS\\Fonts\\LATINWD.TTF',
'C:\\WINDOWS\\Fonts\\HARLOWSI.TTF',
...
'C:\\WINDOWS\\Fonts\\MOD20.TTF',
'C:\\WINDOWS\\Fonts\\georgiaz.ttf',
'C:\\WINDOWS\\Fonts\\wingding.ttf',
'C:\\Windows\\Fonts\\LFAXD.TTF',
'C:\\Windows\\Fonts\\BOD_BLAI.TTF']
# 2. 원하는 위치에서 폰트 불러오기
from pathlib import Path
fpath2 = Path("./fonts/SANGJU Haerye.ttf")
사인/코사인 Plot
# 사인/코사인
x = np.linspace(0, 10, 101)
y1 = np.sin(x)
y2 = np.cos(x)
plt.rcParams["figure.dpi"] = 200 # 도형 해상도(크기) 2배 확대
plt.plot(x, y1, lw = 4, color= "red", label = "sin")
plt.plot(x, y2, lw = 4, color= "blue", label = "cos")
plt.xlabel("x-축 라벨", font=fpath)
plt.xticks(fontsize = 16)
plt.ylabel("y-축 라벨", font=fpath)
plt.yticks(fontsize = 16)
plt.title("사인, 코사인 그래프", font=fpath2)
plt.show()
다양한 함수 형태
fig, ( (ax1, ax2), (ax3, ax4) ) = plt.subplots(2, 2)
ax1.plot(x, y)
ax1.set_yscale("linear")
ax2.plot(x, y)
ax2.set_yscale("log")
ax3.plot(x, y)
ax3.set_yscale("symlog")
ax4.plot(x, y)
ax4.set_yscale("logit")
plt.style.use("Solarize_Light2")
plt.show()
다음 데이터 시각화 실습 바로가기.
댓글남기기