IO
Matplotlib: Python 환경에서 데이터를 시각화하는데 가장 널리 쓰이는 라이브러리 중 하나이다. MATLAB과 유사한 환경을 제공해주는 pyplot 모듈을 활용한 인터페이스가 널리쓰인다. 아래 예시들을 함께 살펴보자.
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4] # list 혹은 numpy array 모두 활용 가능
y = [0, 1, 4, 9, 16]
plt.plot(x, y) # 선 그래프
plt.title("Basic Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
x = [5, 7, 8, 7, 6, 9, 5, 4, 5, 6]
y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78]
plt.scatter(x, y, color='red')
plt.title("Scatter Plot")
각 데이터 세트 (선, 점 등)에 라벨을 부여하고, 이를 레전드(legend) 함수를 활용해 그래프를 꾸밀 수 있다.
plt.plot([1,2,3],[1,4,9], label=r'$y = x^2$')
plt.plot([1,2,3],[1,2,3], label=r'$y = x$')
plt.legend() ## legend
plt.subplot을 활용해서 행렬 행태의 그래프 모임을 그릴 수 있다.
plt.subplot(1, 2, 1) # 1행 2열 중 첫 번째
plt.plot([1,2,3],[1,4,9])
plt.title("Left")
plt.subplot(1, 2, 2) # 두 번째
plt.plot([1,2,3],[1,2,3])
plt.title("Right")
plt환경을 조금 더 상세히 살펴보면, figure와 axis 객체가 사용되는 것을 알 수 있다. figure는 그림을 그리는 캔버스, axis는 그래프가 시각화되는 좌표계라 볼 수 있다.
따라서 한 Figure 안에 여러 개의 axes가 삽입될 수 있다. 아래 예제를 살펴보자.
import matplotlib.pyplot as plt
# Figure(도화지), Axes(좌표 영역) 생성
fig, ax = plt.subplots()
x = [0, 1, 2, 3, 4]
y1 = [0, 1, 4, 9, 16]
y2 = [9, 8, 7, 6, 5]
# ax 객체를 활용해 데이터 플롯
ax.plot(x, y1, label="y = x^2", color="blue")
ax.plot(x, y2, label="y =-x+9", color="red")
# 그래프 꾸미기
ax.set_title("Figure & Axes Example")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.legend()
ax.grid(True)
plt.show()
혹은 각각을 다른 axis에 그리기
import matplotlib.pyplot as plt
# Figure(도화지), Axes(좌표 영역) 생성
fig=plt.figure(figsize=(9,3))
ax1=fig.add_subplot(121)
ax2=fig.add_subplot(122)
x = [0, 1, 2, 3, 4]
y1 = [0, 1, 4, 9, 16]
y2 = [9, 8, 7, 6, 5]
# ax 객체를 활용해 데이터 플롯
ax1.plot(x, y1, label="y = x^2", color="blue")
ax2.plot(x, y2, label="y =-x+9", color="red")
# 그래프 꾸미기
ax1.set_title("Axis 1")
ax1.set_xlabel("X-axis")
ax1.set_ylabel("Y-axis")
ax1.legend()
ax1.grid(True)
ax2.set_title("Axis 2")
ax2.set_xlabel("X-axis")
ax2.set_ylabel("Y-axis")
ax2.legend()
ax2.grid(True)
plt.show()
linspace, logspace등과 결합하면 여러 1D 그래프를 손쉽게 그릴 수 있다. 예를 들어 $y=x^2$을 $x\in[-10,10]$을 그리자면import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(-10,10) # [-10,10] 범위내의 50 포인트
y=x**2 ## NumPy의 element-wise operation을 기억하자.
plt.plot(x,y)
결과를 살펴보자.
위 예제를 응용하여 아래 실습을 수행해보자. 범위 내의 아래 삼각함수를 그려보자.
\(y=\tan(\theta), \text{ with } \theta\in\big[-\frac{\pi}{2},\frac{\pi}{2}\big]\)
다음 압축파일을 풀어서, 파일 하나를 살펴보자 - 예를 들어 00_DD_WZ_01.csv 위 데이터 파일을 활용해