Matplotlib에서 서브플롯 크기를 조정하는 방법
다음 구문을 사용하여 Matplotlib에서 하위 그림의 크기를 조정할 수 있습니다.
#specify one size for all subplots fig, ax = plt. subplots (2, 2, figsize=(10,7)) #specify individual sizes for subplots fig, ax = plt. subplots (1, 2, gridspec_kw={' width_ratios ': [3, 1]})
다음 예에서는 이 구문을 실제로 사용하는 방법을 보여줍니다.
예 1: 모든 하위 그림의 크기 지정
다음 코드는 모든 하위 플롯의 크기를 지정하는 방법을 보여줍니다.
import matplotlib. pyplot as plt #define subplots fig, ax = plt. subplots (2, 2, figsize=(10,7)) fig. tight_layout () #define data x = [1, 2, 3] y = [7, 13, 24] #create subplots ax[0, 0]. plot (x,y,color=' red ') ax[0, 1]. plot (x,y,color=' blue ') ax[1, 0]. plot (x,y,color=' green ') ax[1, 1]. plot (x,y,color=' purple ')
figsize 인수의 값을 변경하여 하위 추적의 크기를 쉽게 변경할 수 있습니다.
import matplotlib. pyplot as plt #define subplots fig, ax = plt. subplots (2, 2, figsize=(5,5)) fig. tight_layout () #define data x = [1, 2, 3] y = [7, 13, 24] #create subplots ax[0, 0]. plot (x,y,color=' red ') ax[0, 1]. plot (x,y,color=' blue ') ax[1, 0]. plot (x,y,color=' green ') ax[1, 1]. plot (x,y,color=' purple ')
예 2: 개별 하위 그림 의 크기 지정
다음 코드는 개별 서브플롯에 대해 서로 다른 크기를 지정하는 방법을 보여줍니다.
import matplotlib. pyplot as plt #define subplots fig, ax = plt. subplots (1, 2, gridspec_kw={' width_ratios ': [3, 1]}) fig. tight_layout () #define data x = [1, 2, 3] y = [7, 13, 24] #create subplots ax[0]. plot (x,y,color=' red ') ax[1]. plot (x,y,color=' blue ')
width_ratios 인수의 값을 변경하여 하위 추적의 크기를 쉽게 변경할 수 있습니다.
import matplotlib. pyplot as plt #define subplots fig, ax = plt. subplots (1, 2, gridspec_kw={' width_ratios ': [1, 3]}) fig. tight_layout () #define data x = [1, 2, 3] y = [7, 13, 24] #create subplots ax[0]. plot (x,y,color=' red ') ax[1]. plot (x,y,color=' blue ')
추가 리소스
Matplotlib에서 플롯에 제목을 추가하는 방법
Matplotlib에서 축 범위를 설정하는 방법
Matplotlib에서 X축 값을 설정하는 방법