matplotlib 범례에서 텍스트 정렬을 수행하려면 다음 단계를 수행할 수 있습니다. -
-
Figure 크기를 설정하고 서브플롯 사이와 주변의 패딩을 조정합니다.
-
numpy를 사용하여 x 데이터 포인트를 만듭니다.
-
플롯 x, sin(x) 및 cos(x) plot() 사용 방법.
-
legend()를 사용하여 범례 배치 메소드를 초기화하고 메소드를 초기화합니다.
-
legend.get_texts() 반복 수평 정렬을 설정하는 방법입니다.
-
그림을 표시하려면 show()를 사용하세요. 방법.
예시
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-5, 5, 100)
plt.plot(x, np.sin(x), label="$y=sin(x)$")
plt.plot(x, np.cos(x), label="$y=cos(x)$")
legend = plt.legend(loc='upper right')
for t in legend.get_texts():
t.set_ha('left')
plt.show() 출력
