Matplotlib에서 fig.add_subplot을 사용하는 방법
다음 기본 구문을 사용하여 Matplotlib에서 하위 플롯을 생성할 수 있습니다.
import matplotlib. pyplot as plt #define figure fig = plt. figure () #add first subplot in layout that has 3 rows and 2 columns fig. add_subplot (321) #add fifth subplot in layout that has 3 rows and 2 columns fig. add_subplot (325) ...
다음 예에서는 이 구문을 실제로 사용하는 방법을 보여줍니다.
예 1: 균일한 레이아웃으로 하위 플롯 추가
다음 코드는 3개의 행과 2개의 열이 있는 레이아웃에 6개의 하위 그림을 생성하는 방법을 보여줍니다.
import matplotlib. pyplot as plt #define figure fig = plt. figure () #add subplots fig. add_subplot (321). set_title (' 321 ') fig. add_subplot (322). set_title (' 322 ') fig. add_subplot (323). set_title (' 323 ') fig. add_subplot (324). set_title (' 324 ') fig. add_subplot (325). set_title (' 325 ') fig. add_subplot (326). set_title (' 326 ') #display plots plt. show ()
결과는 3개의 행과 2개의 열이 있는 레이아웃에 6개의 하위 플롯이 표시됩니다.
예 2: 고르지 않은 레이아웃이 있는 하위 플롯 추가
다음 코드는 다음과 같이 4개의 하위 플롯을 생성하는 방법을 보여줍니다.
- 3개의 플롯이 3행 2열의 그리드에 생성됩니다.
- 네 번째 플롯은 1행 2열의 그리드로 생성됩니다.
import matplotlib. pyplot as plt #define figure fig = plt. figure () #add subplots fig. add_subplot (321). set_title (' 321 ') fig. add_subplot (323). set_title (' 323 ') fig. add_subplot (325). set_title (' 325 ') fig. add_subplot (122). set_title (' 122 ') #display plots plt. show ()
최종 결과는 3×2 격자에 세 개의 하위 그림이 표시되고 마지막 하위 그림은 1×2 격자에 표시됩니다.
추가 리소스
다음 튜토리얼에서는 Matplotlib에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.
Matplotlib 서브플롯 사이의 간격을 조정하는 방법
Matplotlib에서 서브플롯 크기를 조정하는 방법
Matplotlib의 서브플롯에 제목을 추가하는 방법