Matplotlib에서 글꼴을 변경하는 방법(예제 포함)


다음 방법 중 하나를 사용하여 Matplotlib에서 글꼴 모음을 변경할 수 있습니다.

방법 1: 모든 텍스트의 글꼴 변경

 import matplotlib

matplotlib. rcParams [' font.family '] = ' monospace '

방법 2: 제목 및 축 레이블의 글꼴 변경

 import matplotlib. pylot as plt

mono_font = {' fontname ': ' monospace '}
serif_font = {' fontname ': ' serif '}

plt. title (' Title of Plot ', ** mono_font)
plt. xlabel (' X Label ', ** serif_font)

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

방법 1: 모든 텍스트의 글꼴 변경

다음 코드는 Matplotlib 플롯의 모든 텍스트에 대한 글꼴 모음을 변경하는 방법을 보여줍니다.

 import matplotlib
import matplotlib. pyplot as plt

#define font family to use for all text
matplotlib. rcParams [' font.family '] = ' monospace '

#define x and y
x = [1, 4, 10]
y = [5, 9, 27]

#create line plot
plt. plot (x, y)

#add title and axis labels
plt. title (' Title of Plot ')
plt. xlabel (' XLabel ')
plt. ylabel (' Y Label ')

#displayplot
plt. show () 

두 축의 제목과 레이블에는 “monospace” 글꼴이 있습니다. 이는 rcParams 인수에 지정한 글꼴 계열이기 때문입니다.

방법 2: 제목 및 축 레이블의 글꼴 변경

다음 코드는 제목 및 축 레이블에 고유한 글꼴 모음을 지정하는 방법을 보여줍니다.

 import matplotlib. pyplot as plt

#define font families to use
mono_font = {' fontname':'monospace '}
serif_font = {' fontname':'serif '}

#define x and y
x = [1, 4, 10]
y = [5, 9, 27]

#create plot of x and y
plt. plot (x, y)

#specify title and axis labels with custom font families
plt. title (' Title of Plot ', ** mono_font)
plt. xlabel (' X Label ', ** serif_font)
plt. ylabel (' Y Label ', ** serif_font)

#displayplot
plt. show ()

제목은 “monospace” 글꼴 모음을 사용하는 반면 X 및 Y 축 레이블은 “Serif” 글꼴 모음을 사용합니다.

참고 : 여기 에서 Matplotlib에서 사용할 수 있는 글꼴 모음의 전체 목록을 찾을 수 있습니다.

추가 리소스

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

Matplotlib 플롯에서 글꼴 크기를 변경하는 방법
Matplotlib에서 범례 글꼴 크기를 변경하는 방법
Matplotlib에서 체크 표시 라벨의 글꼴 크기를 설정하는 방법

의견을 추가하다

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