Matplotlib에서 축 레이블 위치를 조정하는 방법
Matplotlib에서 축 레이블 위치를 조정하려면 다음 기본 구문을 사용할 수 있습니다.
#adjust y-axis label position ax. yaxis . set_label_coords (-.1, .5) #adjust x-axis label position ax. xaxis . set_label_coords (.5, -.1)
다음 예에서는 이 구문을 실제로 사용하는 방법을 보여줍니다.
예 1: X축의 라벨 위치 조정
다음 코드는 Matplotlib에서 플롯을 생성하고 x축의 레이블 위치 위치만 조정하는 방법을 보여줍니다.
import matplotlib. pyplot as plt #define data x = [1, 2, 3, 4, 5, 6] y = [4, 5, 8, 14, 24, 19] #create scatterplot fig, ax = plt. subplots () ax. scatter (x,y) #add axis labels ax. set_ylabel (' Y-Axis Label ') ax. set_xlabel (' X-Axis Label ') #adjust position of x-axis label ax. xaxis . set_label_coords (.9, -.1)
축 좌표계에서는 (0, 0) 을 사용하여 플롯의 왼쪽 하단 모서리를 나타내고, (0.5, 0.5)를 사용하여 중심을 나타내고, (1, 1)을 사용 하여 오른쪽 상단 모서리를 나타냅니다.
예 2: Y축의 레이블 위치 조정
다음 코드는 Matplotlib에서 플롯을 생성하고 Y축 레이블 위치의 위치만 조정하는 방법을 보여줍니다.
import matplotlib. pyplot as plt #define data x = [1, 2, 3, 4, 5, 6] y = [4, 5, 8, 14, 24, 19] #create scatterplot fig, ax = plt. subplots () ax. scatter (x,y) #add axis labels ax. set_ylabel (' Y-Axis Label ') ax. set_xlabel (' X-Axis Label ') #adjust position of x-axis label ax. yaxis . set_label_coords (-.1, .1)
예 3: 두 축의 라벨 위치 조정
다음 코드는 Matplotlib에서 플롯을 생성하고 두 축의 레이블 위치 위치를 조정하는 방법을 보여줍니다.
import matplotlib. pyplot as plt #define data x = [1, 2, 3, 4, 5, 6] y = [4, 5, 8, 14, 24, 19] #create scatterplot fig, ax = plt. subplots () ax. scatter (x,y) #add axis labels ax. set_ylabel (' Y-Axis Label ') ax. set_xlabel (' X-Axis Label ') #adjust position of both axis labels ax. yaxis . set_label_coords (-.1, .1) ax. xaxis . set_label_coords (.9, -.1)
추가 리소스
다음 튜토리얼에서는 Matplotlib에서 다른 일반적인 기능을 수행하는 방법을 설명합니다.
Matplotlib에서 축을 숨기는 방법
Matplotlib에서 체크 표시 라벨을 회전하는 방법
Matplotlib에서 틱 수를 변경하는 방법