Matplotlib 텍스트 정렬

Beginner

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

소개

Matplotlib 는 Python 프로그래밍 언어와 수치 수학 확장 NumPy 를 위한 플로팅 라이브러리입니다. 이 튜토리얼에서는 Matplotlib 에서 텍스트 정렬에 대해 중점적으로 다룹니다.

VM 팁

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

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

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

사각형 생성

matplotlib.patches 모듈의 Rectangle() 함수를 사용하여 플롯에 사각형을 생성하는 것으로 시작합니다. 사각형을 생성한 후, set_xlim()set_ylim() 함수를 사용하여 가로 및 세로 범위를 설정합니다. 마지막으로, 사각형을 플롯에 추가합니다.

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig, ax = plt.subplots()

## Build a rectangle in axes coords
left, width = .25, .5
bottom, height = .25, .5
right = left + width
top = bottom + height
p = Rectangle((left, bottom), width, height, fill=False)
ax.add_patch(p)

## Set the horizontal and vertical limits
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.show()

사각형에 텍스트 추가

이 단계에서는 text() 함수를 사용하여 사각형에 텍스트를 추가합니다. 텍스트의 가로 및 세로 정렬은 각각 horizontalalignmentverticalalignment 매개변수를 사용하여 설정됩니다.

## Add text to the rectangle
ax.text(left, bottom, 'left top',
        horizontalalignment='left',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(left, bottom, 'left bottom',
        horizontalalignment='left',
        verticalalignment='bottom',
        transform=ax.transAxes)

ax.text(right, top, 'right bottom',
        horizontalalignment='right',
        verticalalignment='bottom',
        transform=ax.transAxes)

ax.text(right, top, 'right top',
        horizontalalignment='right',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(right, bottom, 'center top',
        horizontalalignment='center',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(left, 0.5 * (bottom + top), 'right center',
        horizontalalignment='right',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(left, 0.5 * (bottom + top), 'left center',
        horizontalalignment='left',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(0.5 * (left + right), 0.5 * (bottom + top), 'middle',
        horizontalalignment='center',
        verticalalignment='center',
        transform=ax.transAxes)

ax.text(right, 0.5 * (bottom + top), 'centered',
        horizontalalignment='center',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(left, top, 'rotated\nwith newlines',
        horizontalalignment='center',
        verticalalignment='center',
        rotation=45,
        transform=ax.transAxes)

plt.show()

플롯 사용자 정의

이 단계에서는 축 레이블을 추가하고 축 선을 제거하여 플롯을 사용자 정의합니다.

## Customize the plot
ax.set_axis_off()
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_title('Text Alignment in Matplotlib')
plt.show()

요약

이 튜토리얼에서는 Matplotlib 에서 텍스트를 정렬하는 방법을 배웠습니다. text() 함수를 사용하여 사각형에 텍스트를 추가하고, horizontalalignmentverticalalignment 매개변수를 사용하여 각각 가로 및 세로 정렬을 설정했습니다. 또한 축 레이블을 추가하고 축 선을 제거하여 플롯을 사용자 정의했습니다.