Matplotlib에서 축 제한을 얻는 방법(예제 포함)


Matplotlib에서 플롯의 x 및 y 축에 대한 축 제한을 얻으려면 다음 구문을 사용할 수 있습니다.

 import matplotlib. pyplot as plt

#get x-axis and y-axis limits
xmin, xmax, ymin, ymax = plt. axis ()

#print axis limits
print (xmin, xmax, ymin, ymax)

다음 예에서는 실제로 이 구문을 사용하는 방법을 보여줍니다.

예: Matplotlib에서 축 제한을 얻는 방법

Matplotlib에서 다음과 같은 산점도를 생성한다고 가정합니다.

 import matplotlib. pyplot as plt

#define x and y
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [1, 5, 9, 15, 24, 39, 35, 35, 40, 41]

#create scatter plot of x vs. y
plt. scatter (x,y) 

다음 구문을 사용하여 산점도의 x축과 y축에 대한 축 제한을 얻을 수 있습니다.

 import matplotlib. pyplot as plt

#define x and y
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [1, 5, 9, 15, 24, 39, 35, 35, 40, 41]

#create scatter plot of x vs. y
plt. scatter (x,y)

#get x-axis and y-axis limits
xmin, xmax, ymin, ymax = plt. axis ()

#print axis limits
print (xmin, xmax, ymin, ymax)

0.55 10.45 -1.0 43.0

결과에서 우리는 다음을 볼 수 있습니다:

  • x축 최소값: 0.55
  • x축 최대값: 10.45
  • y축 최소값: -1.0
  • y축 최대값: 43.0

이 값은 위의 산점도에 표시되는 축 제한에 해당합니다.

원하는 경우 annotate() 함수를 사용하여 이러한 축 제한을 텍스트 값으로 플롯에 추가할 수도 있습니다.

 import matplotlib. pyplot as plt

#define x and y
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [1, 5, 9, 15, 24, 39, 35, 35, 40, 41]

#create scatter plot of x vs. y
plt. scatter (x,y)

#get x-axis and y-axis limits
xmin, xmax, ymin, ymax = plt. axis ()

#print axis limits
lims = ' xmin: ' + str(round(xmin, 2 )) + ' \n ' + \
       ' xmax: ' + str(round(xmax, 2 )) + ' \n ' + \
       ' ymin: ' + str(round(ymin, 2 )) + ' \n ' + \
       ' ymax: ' + str(round(ymax, 2 ))

#add axis limits to plot at (x,y) coordinate (1.35)
plt. annotate (lims, ( 1 , 35 ))

Matplotlib는 축 제한을 얻습니다.

추가 리소스

다음 튜토리얼에서는 Matplotlib에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.

Matplotlib에서 축 틱을 설정하는 방법
Matplotlib에서 플롯 크기를 늘리는 방법
Matplotlib 플롯에 텍스트를 추가하는 방법

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다