간단한 축 패드

Beginner

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

소개

이 랩에서는 Matplotlib 의 add_floating_axis 함수를 사용하여 플롯에 플로팅 축을 추가하는 방법을 배웁니다. 이를 통해 플롯에 대한 추가 정보를 표시할 수 있습니다. 특히, 눈금 레이블과 축 레이블의 패딩을 조정하는 방법과 플로팅 축의 눈금 위치를 조정하는 방법을 배우게 됩니다.

VM 팁

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

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

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

라이브러리 임포트

먼저, matplotlib.pyplot, numpy, 그리고 mpl_toolkits.axisartist를 포함한 필요한 라이브러리를 임포트합니다.

import matplotlib.pyplot as plt
import numpy as np
import mpl_toolkits.axisartist as axisartist

축 설정 함수 정의

다음으로, 플롯의 극좌표 투영을 설정하는 setup_axes() 함수를 정의합니다. 이 함수는 GridHelperCurveLinear를 사용하여 직사각형 상자에 극좌표 투영을 생성합니다. 또한 플롯의 범위를 설정하고 ax1 객체를 반환합니다.

def setup_axes(fig, rect):
    ## Define the PolarAxes transform and the extreme finder
    tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform()
    extreme_finder = angle_helper.ExtremeFinderCycle(20, 20, lon_cycle=360, lat_cycle=None, lon_minmax=None, lat_minmax=(0, np.inf))

    ## Define the grid locators and formatters
    grid_locator1 = angle_helper.LocatorDMS(12)
    grid_locator2 = grid_finder.MaxNLocator(5)
    tick_formatter1 = angle_helper.FormatterDMS()

    ## Define the GridHelperCurveLinear
    grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=grid_locator2, tick_formatter1=tick_formatter1)

    ## Create the axis object and set its limits
    ax1 = fig.add_subplot(rect, axes_class=axisartist.Axes, grid_helper=grid_helper)
    ax1.axis[:].set_visible(False)
    ax1.set_aspect(1.)
    ax1.set_xlim(-5, 12)
    ax1.set_ylim(-5, 10)

    return ax1

부동 축 추가 함수 정의

플롯에 부동 축을 추가하는 add_floating_axis 함수를 정의합니다. 이 함수는 ax1 객체를 인수로 받아 axis 객체를 반환합니다.

def add_floating_axis(ax1):
    ## Define the floating axis
    ax1.axis["lat"] = axis = ax1.new_floating_axis(0, 30)
    axis.label.set_text(r"$\theta = 30^{\circ}$")
    axis.label.set_visible(True)

    return axis

눈금 레이블에 패딩 추가

이 단계에서는 부동 축의 눈금 레이블에 패딩을 추가합니다. 이는 major_ticklabels 객체의 pad 속성을 원하는 패딩 값으로 설정하여 수행할 수 있습니다.

## Add Padding to Tick Labels
fig = plt.figure(figsize=(9, 3.))
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99, wspace=0.01, hspace=0.01)

ax1 = setup_axes(fig, rect=121)
axis = add_floating_axis(ax1)

ax1 = setup_axes(fig, rect=122)
axis = add_floating_axis(ax1)
axis.major_ticklabels.set_pad(10)

plt.show()

축 레이블 패딩 조정

이 단계에서는 부동 축의 축 레이블 패딩을 조정합니다. 이는 label 객체의 pad 속성을 원하는 패딩 값으로 설정하여 수행할 수 있습니다.

## Adjust Axis Label Padding
fig = plt.figure(figsize=(9, 3.))
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99, wspace=0.01, hspace=0.01)

ax1 = setup_axes(fig, rect=121)
axis = add_floating_axis(ax1)

ax1 = setup_axes(fig, rect=122)
axis = add_floating_axis(ax1)
axis.label.set_pad(20)

plt.show()

눈금 위치 조정

이 단계에서는 부동 축의 눈금 위치를 조정합니다. 이는 major_ticks 객체의 tick_out 속성을 True로 설정하여 수행할 수 있습니다.

## Adjust Tick Position
fig = plt.figure(figsize=(9, 3.))
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99, wspace=0.01, hspace=0.01)

ax1 = setup_axes(fig, rect=121)
axis = add_floating_axis(ax1)

ax1 = setup_axes(fig, rect=122)
axis = add_floating_axis(ax1)
axis.major_ticks.set_tick_out(True)

plt.show()

요약

이 랩에서는 Matplotlib 에서 add_floating_axis 함수를 사용하여 플롯에 부동 축을 추가하는 방법을 배웠습니다. 또한 눈금 레이블과 축 레이블의 패딩을 조정하는 방법과 부동 축의 눈금 위치를 조정하는 방법도 배웠습니다. 이 랩을 마치면 플롯에 대한 추가 정보를 표시하는 부동 축이 있는 사용자 정의 플롯을 만들 수 있어야 합니다.