Matplotlib 글꼴 속성 사용자 정의

Beginner

This tutorial is from open-source community. Access the source code

소개

이 랩에서는 Matplotlib 에서 다양한 글꼴 속성을 사용하여 플롯의 시각적 외관을 향상시키는 방법을 배우게 됩니다.

VM 팁

VM 시작이 완료되면 왼쪽 상단 모서리를 클릭하여 Notebook 탭으로 전환하여 실습을 위해 Jupyter Notebook에 액세스하십시오.

때로는 Jupyter Notebook 이 로딩을 완료하는 데 몇 초 정도 기다려야 할 수 있습니다. Jupyter Notebook 의 제한으로 인해 작업의 유효성 검사는 자동화될 수 없습니다.

학습 중에 문제가 발생하면 언제든지 Labby 에게 문의하십시오. 세션 후 피드백을 제공해주시면 문제를 신속하게 해결해 드리겠습니다.

설정

시작하기 전에 필요한 라이브러리를 가져오고 플롯을 설정해야 합니다.

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

fig = plt.figure()
alignment = {'horizontalalignment': 'center', 'verticalalignment': 'baseline'}
yp = [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]
heading_font = FontProperties(size='large')

Family 옵션

첫 번째로 살펴볼 글꼴 속성은 family 옵션입니다. 이 속성을 사용하면 플롯에 사용되는 글꼴 패밀리 (family) 를 설정할 수 있습니다.

## Show family options
fig.text(0.1, 0.9, 'family', fontproperties=heading_font, **alignment)
families = ['serif', 'sans-serif', 'cursive', 'fantasy', 'monospace']
for k, family in enumerate(families):
    font = FontProperties()
    font.set_family(family)
    fig.text(0.1, yp[k], family, fontproperties=font, **alignment)

Style 옵션

두 번째로 살펴볼 글꼴 속성은 style 옵션입니다. 이 속성을 사용하면 플롯에 사용되는 글꼴 스타일 (style) 을 설정할 수 있습니다.

## Show style options
styles = ['normal', 'italic', 'oblique']
fig.text(0.3, 0.9, 'style', fontproperties=heading_font, **alignment)
for k, style in enumerate(styles):
    font = FontProperties()
    font.set_family('sans-serif')
    font.set_style(style)
    fig.text(0.3, yp[k], style, fontproperties=font, **alignment)

Variant 옵션

세 번째로 살펴볼 글꼴 속성은 variant 옵션입니다. 이 속성을 사용하면 플롯에 사용되는 글꼴 변형 (variant) 을 설정할 수 있습니다.

## Show variant options
variants = ['normal', 'small-caps']
fig.text(0.5, 0.9, 'variant', fontproperties=heading_font, **alignment)
for k, variant in enumerate(variants):
    font = FontProperties()
    font.set_family('serif')
    font.set_variant(variant)
    fig.text(0.5, yp[k], variant, fontproperties=font, **alignment)

Weight 옵션

네 번째로 살펴볼 글꼴 속성은 weight 옵션입니다. 이 속성을 사용하면 플롯에 사용되는 글꼴 굵기 (weight) 를 설정할 수 있습니다.

## Show weight options
weights = ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black']
fig.text(0.7, 0.9, 'weight', fontproperties=heading_font, **alignment)
for k, weight in enumerate(weights):
    font = FontProperties()
    font.set_weight(weight)
    fig.text(0.7, yp[k], weight, fontproperties=font, **alignment)

Size 옵션

다섯 번째로 살펴볼 글꼴 속성은 size 옵션입니다. 이 속성을 사용하면 플롯에 사용되는 글꼴 크기 (size) 를 설정할 수 있습니다.

## Show size options
sizes = [
    'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large']
fig.text(0.9, 0.9, 'size', fontproperties=heading_font, **alignment)
for k, size in enumerate(sizes):
    font = FontProperties()
    font.set_size(size)
    fig.text(0.9, yp[k], size, fontproperties=font, **alignment)

Bold Italic

마지막으로 살펴볼 글꼴 속성은 style 과 weight 옵션의 조합입니다. 이 속성을 사용하면 플롯에 사용되는 글꼴 스타일과 굵기를 설정할 수 있습니다.

## Show bold italic
font = FontProperties(style='italic', weight='bold', size='x-small')
fig.text(0.3, 0.1, 'bold italic', fontproperties=font, **alignment)
font = FontProperties(style='italic', weight='bold', size='medium')
fig.text(0.3, 0.2, 'bold italic', fontproperties=font, **alignment)
font = FontProperties(style='italic', weight='bold', size='x-large')
fig.text(0.3, 0.3, 'bold italic', fontproperties=font, **alignment)

요약

이 랩에서는 Matplotlib 에서 다양한 글꼴 속성을 사용하여 플롯의 시각적 외관을 향상시키는 방법을 배웠습니다. 글꼴 패밀리 (font family), 스타일 (style), 변형 (variant), 굵기 (weight) 및 크기 (size) 를 설정하여 특정 요구 사항에 맞게 플롯의 글꼴을 사용자 정의할 수 있습니다.