Matplotlib의 서브플롯에 텍스트를 추가하는 방법
다음 구문을 사용하여 Matplotlib의 특정 하위 그림에 텍스트를 추가할 수 있습니다.
import matplotlib. pyplot as plt #define subplot layout fig, ax = plt. subplots (2, 1, figsize=(7,4)) #add text at specific locations in subplots ax[0]. text (1.5, 20, ' Here is some text in the first subplot ') ax[1]. text (2, 10, ' Here is some text in the second subplot ')
이 특정 예에서는 (x,y) 좌표 (1,5,20) 의 첫 번째 서브플롯에 텍스트를 추가하고 (x,y) 좌표 (2,10) 의 두 번째 서브플롯에 텍스트를 추가합니다.
다음 예에서는 실제로 이 구문을 사용하는 방법을 보여줍니다.
예: Matplotlib의 서브플롯에 텍스트 추가
다음 코드는 Matplotlib에서 두 개의 행과 하나의 열이 있는 레이아웃으로 배열된 두 개의 하위 플롯을 만드는 방법을 보여줍니다.
import matplotlib. pyplot as plt #define subplot layout fig, ax = plt. subplots (2, 1, figsize=(7,4)) 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 ')
다음 구문을 사용하여 각 서브플롯의 특정 위치에 텍스트를 추가할 수 있습니다.
import matplotlib. pyplot as plt #define subplot layout fig, ax = plt. subplots (2, 1, figsize=(7,4)) 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 ') #add text at specific locations in subplots ax[0]. text (1.5, 20, ' Here is some text in the first subplot ') ax[1]. text (2, 10, ' Here is some text in the second subplot ')
우리가 지정한 (x,y) 좌표의 각 서브플롯에 텍스트가 추가되었습니다.
ax[0]을 사용하여 첫 번째 서브플롯을 참조하고 ax[1]을 사용하여 두 번째 서브플롯을 참조했습니다.
그런 다음 text() 함수를 사용하여 (x, y) 좌표와 각 서브플롯에 사용할 특정 텍스트를 지정했습니다.
추가 리소스
다음 튜토리얼에서는 Matplotlib에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.
Matplotlib의 서브플롯에 제목을 추가하는 방법
Matplotlib에서 서브플롯 크기를 조정하는 방법
Matplotlib 서브플롯 사이의 간격을 조정하는 방법