Matplotlib 사용자 정의 그리드 축 생성

Beginner

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

소개

이 랩에서는 mpl_toolkits.axes_grid1 모듈을 사용하여 Matplotlib 으로 사용자 정의 축 그리드를 만드는 방법을 배웁니다. 고정된 축 크기와 패딩을 사용하는 예제와, 크기 조절이 가능한 축 크기와 고정된 패딩을 사용하는 예제, 두 가지를 만들 것입니다.

VM 팁

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

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

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

필요한 라이브러리 임포트

시각화를 위해 matplotlib.pyplot를, 사용자 정의 축 그리드를 만들기 위해 mpl_toolkits.axes_grid1을 임포트하는 것으로 시작합니다.

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import Divider, Size

헬퍼 함수 정의

축의 중앙에 레이블을 배치하고 축 눈금을 제거하는 데 사용될 헬퍼 함수 label_axes()를 정의합니다.

def label_axes(ax, text):
    """Place a label at the center of an Axes, and remove the axis ticks."""
    ax.text(.5, .5, text, transform=ax.transAxes,
            horizontalalignment="center", verticalalignment="center")
    ax.tick_params(bottom=False, labelbottom=False,
                   left=False, labelleft=False)

고정된 크기와 패딩을 가진 사용자 정의 축 그리드 생성

고정된 크기와 패딩을 가진 사용자 정의 축 그리드를 생성합니다. Divider 클래스를 사용하여 축 사각형을 horiz * vert로 지정된 크기를 가진 그리드로 나눕니다. 그런 다음 add_axes() 메서드를 사용하여 그림에 네 개의 축을 추가하고, Divider 클래스의 new_locator() 메서드를 사용하여 각 축의 위치를 지정합니다.

## Sizes are in inches.
horiz = [Size.Fixed(1.), Size.Fixed(.5), Size.Fixed(1.5), Size.Fixed(.5)]
vert = [Size.Fixed(1.5), Size.Fixed(.5), Size.Fixed(1.)]

rect = (0.1, 0.1, 0.8, 0.8)
fig = plt.figure(figsize=(6, 6))
fig.suptitle("Fixed axes sizes, fixed paddings")

div = Divider(fig, rect, horiz, vert, aspect=False)

## The rect parameter will actually be ignored and overridden by axes_locator.
ax1 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
label_axes(ax1, "nx=0, ny=0")
ax2 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
label_axes(ax2, "nx=0, ny=2")
ax3 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
label_axes(ax3, "nx=2, ny=2")
ax4 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
label_axes(ax4, "nx=2, nx1=4, ny=0")

plt.show()

크기 조절 가능한 크기와 고정된 패딩을 가진 사용자 정의 축 그리드 생성

크기 조절 가능한 크기와 고정된 패딩을 가진 또 다른 사용자 정의 축 그리드를 생성합니다. Size.Scaled() 옵션을 사용하여 그림 크기에 따라 크기가 조절되는 축 크기를 지정합니다. 나머지 단계는 이전 예제와 유사합니다.

## Sizes are in inches.
horiz = [Size.Scaled(1.5), Size.Fixed(.5), Size.Scaled(1.), Size.Scaled(.5)]
vert = [Size.Scaled(1.), Size.Fixed(.5), Size.Scaled(1.5)]

rect = (0.1, 0.1, 0.8, 0.8)
fig = plt.figure(figsize=(6, 6))
fig.suptitle("Scalable axes sizes, fixed paddings")

div = Divider(fig, rect, horiz, vert, aspect=False)

## The rect parameter will actually be ignored and overridden by axes_locator.
ax1 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
label_axes(ax1, "nx=0, ny=0")
ax2 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
label_axes(ax2, "nx=0, ny=2")
ax3 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
label_axes(ax3, "nx=2, ny=2")
ax4 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
label_axes(ax4, "nx=2, nx1=4, ny=0")

plt.show()

요약

이 랩에서는 mpl_toolkits.axes_grid1 모듈을 사용하여 Matplotlib 으로 사용자 정의 축 그리드를 만드는 방법을 배웠습니다. 고정된 축 크기와 패딩을 사용하는 예제와 크기 조절 가능한 축 크기와 고정된 패딩을 사용하는 예제, 두 가지를 만들었습니다. Divider 클래스를 사용하여 축 사각형을 horiz * vert로 지정된 크기를 가진 그리드로 나누고, add_axes() 메서드와 Divider 클래스의 new_locator() 메서드를 사용하여 그림에 축을 추가했습니다.