Matplotlib 서브플롯 사이의 간격을 조정하는 방법
종종 하위 플롯을 사용하여 Matplotlib에서 여러 플롯을 나란히 표시할 수 있습니다. 불행하게도 이러한 하위 플롯은 기본적으로 겹치는 경향이 있습니다.
이 문제를 해결하는 가장 쉬운 방법은 Matplotlib Tight_layout() 함수를 사용하는 것입니다. 이 튜토리얼에서는 이 기능을 실제로 사용하는 방법을 설명합니다.
서브플롯 생성
2열과 2행의 4개 서브플롯으로 구성된 다음 배열을 고려해보세요.
import matplotlib.pyplot as plt #define subplots fig, ax = plt. subplots (2, 2) #display subplots plt. show ()
서브플롯이 어떻게 약간 겹치는지 확인하세요.
Tight_layout()을 사용하여 서브플롯 간격 조정
이 중첩 문제를 해결하는 가장 쉬운 방법은 Matplotlib Tight_layout() 함수를 사용하는 것입니다.
import matplotlib.pyplot as plt #define subplots fig, ax = plt. subplots (2, 2) fig. tight_layout () #display subplots plt. show ()
서브플롯 제목의 간격 조정
경우에 따라 각 하위 플롯에 대한 제목을 가질 수도 있습니다. 불행하게도 Tight_layout() 함수조차도 하위 플롯 제목이 겹치는 경향이 있습니다.
import matplotlib.pyplot as plt #define subplots fig, ax = plt. subplots (2, 2) fig. tight_layout () #define subplot titles ax[0, 0]. set_title ('First Subplot') ax[0, 1]. set_title ('Second Subplot') ax[1, 0]. set_title ('Third Subplot') ax[1, 1]. set_title ('Fourth Subplot') #display subplots plt. show ()
이 문제를 해결하는 방법은 h_pad 인수를 사용하여 하위 플롯 사이의 높이 패딩을 늘리는 것입니다.
import matplotlib.pyplot as plt #define subplots fig, ax = plt. subplots (2, 2) fig. tight_layout (h_pad= 2 ) #define subplot titles ax[0, 0]. set_title ('First Subplot') ax[0, 1]. set_title ('Second Subplot') ax[1, 0]. set_title ('Third Subplot') ax[1, 1]. set_title ('Fourth Subplot') #display subplots plt. show ()
전체 제목 간격 조정
전체 제목이 있는 경우 subplots_adjust() 함수를 사용하여 하위 플롯 제목과 겹치지 않는지 확인할 수 있습니다.
import matplotlib.pyplot as plt #define subplots fig, ax = plt. subplots (2, 2) fig. tight_layout (h_pad= 2 ) #define subplot titles ax[0, 0]. set_title ('First Subplot') ax[0, 1]. set_title ('Second Subplot') ax[1, 0]. set_title ('Third Subplot') ax[1, 1]. set_title ('Fourth Subplot') #add overall title and adjust it so that it doesn't overlap with subplot titles fig.suptitle(' Overall Title ') plt.subplots_adjust(top= 0.85 ) #display subplots plt. show ()
여기에서 더 많은 Matplotlib 튜토리얼을 찾을 수 있습니다.