Matplotlib 서브플롯
여러 플롯 표시
이 subplots()
기능을 사용하면 하나의 그림에 여러 플롯을 그릴 수 있습니다.
예시
2개의 플롯을 그립니다.
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x =
np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.show()
결과:
subplots() 함수
이 subplots()
함수는 Figure의 레이아웃을 설명하는 세 개의 인수를 취합니다.
레이아웃은 첫 번째 및 두 번째 인수 로 표시되는 행과 열로 구성됩니다 .
세 번째 인수는 현재 플롯의 인덱스를 나타냅니다.
plt.subplot(1, 2, 1)
#the figure has 1 row, 2 columns, and this plot is the first plot.
plt.subplot(1, 2, 2)
#the figure has 1 row, 2 columns, and this plot is the second plot.
따라서 2개의 행과 1개의 열이 있는 그림을 원하면(즉, 두 개의 플롯이 나란히 표시되지 않고 서로 겹쳐서 표시됨) 다음과 같은 구문을 작성할 수 있습니다.
예시
서로의 위에 2개의 플롯을 그립니다.
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x =
np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 1, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(2, 1, 2)
plt.plot(x,y)
plt.show()
결과:
하나의 그림에 원하는 만큼 플롯을 그릴 수 있습니다. 플롯의 행, 열 및 인덱스 수만 설명하면 됩니다.
예시
6개의 플롯을 그립니다.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0,
1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 1)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(2, 3, 2)
plt.plot(x,y)
x = np.array([0, 1,
2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 3)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 4)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y =
np.array([3, 8, 1, 10])
plt.subplot(2, 3, 5)
plt.plot(x,y)
x
= np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2,
3, 6)
plt.plot(x,y)
plt.show()
결과:
제목
title()
다음 기능 을 사용하여 각 플롯에 제목을 추가할 수 있습니다 .
예시
제목이 있는 2개의 플롯:
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x =
np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")
plt.show()
결과:
슈퍼 타이틀
suptitle()
다음 기능 을 사용하여 전체 그림에 제목을 추가할 수 있습니다 .
예시
전체 그림에 대한 제목 추가:
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x =
np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")
plt.suptitle("MY SHOP")
plt.show()
결과: