Matplotlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
- Figure 객체: 그림을 그리기 위한 (빈) 객체
fig = plt.figure()
fig = plt.figure(figsize = (5,5), layout = 'constrained')
- Figure는 빈 객체일 뿐, 최소 하나이상의 subplot을 생성해야함
- 한번에 하나씩 subplot을 생성하는 법(add_subplot)
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
# 출력 (빈 객체)
fig
- 여전히 ax1 - ax3은 빈 객체. 이제 그림을 그려보자.
# 1변량 trend
ax1.plot(np.random.standard_normal(50).cumsum(), color="black", linestyle="dashed")
# Histogram
ax2.hist(np.random.standard_normal(100), bins=20, color="black", alpha=0.3)
# Satter Plot (산점도)
ax3.scatter(np.arange(30), np.arange(30) + 3 * np.random.standard_normal(30))
# 출력
fig
fig, axes = plt.subplots(2, 2)
axes[0,0].plot(np.random.standard_normal(50).cumsum(),
color="black",
linestyle="dashed")
axes[0,1].hist(np.random.standard_normal(100),
bins=20,
color="black",
alpha=0.3)
axes[1,0].scatter(np.arange(30),
np.arange(30) + 3 * np.random.standard_normal(30))
axes
- matplotlib.pyplot.subplots 주요 옵션
인수 |
설명 |
nrows |
Integer - 서브플롯의 행 수 |
ncols |
Integer - 서브플롯의 열 수 |
sharex |
Logical - 모든 서브플롯이 동일한 x축을 사용할 것인가 여부 |
sharey |
Logical - 모든 서브플롯이 동일한 축y을 사용할 것인가 여부 |
그림의 종류
- Association
- Distribution
- Gridded
- 3-dimensional: Avoid!
M.1. 관계의 표현: $y = f(x)$