HOWTO · Matplotlib

Matplotlib 에서 범례 글꼴 크기를 변경하는 방법

이 튜토리얼에서는 Matplotlib 에서 범례의 글꼴 크기를 설정하는 방법을 소개합니다.

Matplotlib 의 범례에서 텍스트의 글꼴 크기를 설정하는 다른 방법이 있습니다.

rcParams 폰트 크기를 지정하는 방법

rcParams 는 Matplotlib 의 Matplotlib 속성과 기본 스타일을 처리하기위한 사전입니다.

1.plt.rc('legend', fontsize= )방법

fontsize 는 포인트 단위를 갖는 정수이거나 다음과 같은 크기 문자열 일 수 있습니다.

xx - -small
x - small
small
medium
large
x - large
xx - large
plt.rc("legend", fontsize=16)
plt.rc("legend", fontsize="medium")

Matplotlib 범례 글꼴 크기 지정

plt.rcparams.update()메소드

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

plt.plot(x, np.sin(x), label="sin(x)")

params = {"legend.fontsize": 16, "legend.handlelength": 3}

plt.rcParams.update(params)

plt.legend(loc="upper left")

plt.tight_layout()

plt.show()

legend.fontsize 는 범례 글꼴 크기를 지정하고 legend.handlelength 는 범례 핸들의 길이를 글꼴 크기 단위로 지정합니다.

plt.rcParams.update(params)는 위에서 정의한대로 params사전으로 Matplotlib 속성과 스타일을 업데이트합니다.

또는 키를 괄호[]에 넣어서 rcParams 사전을 업데이트 할 수 있습니다.

plt.rcParams["legend.fontsize"] = 16
plt.rcParams["legend.handlelength"] = 16

plt.legend(fontsize= )범례 글꼴 크기를 지정하는 방법

plt.legend(fontsize=)는 각 범례가 생성 될 때 범례 글꼴 크기를 지정할 수 있습니다.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

plt.plot(x, np.sin(x), label="sin(x)")

plt.legend(fontsize=16, loc="upper right")

plt.show()

범례의 prop속성

범례의 prop 속성은 범례의 개별 글꼴 크기를 설정할 수 있습니다. prop 의 값은 matplotlib.font_manager.FontProperties 의 키워드 사전입니다.

plt.legend(prop={"size": 16})

예:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

plt.plot(x, np.sin(x), label="sin(x)")

plt.legend(prop={"size": 16}, loc="best")

plt.show()