단위가 있는 타원

Beginner

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

소개

이 랩에서는 Python Matplotlib 을 사용하여 호 (arc) 로 생성된 타원과 다각형 근사를 비교하는 방법을 배우게 됩니다.

VM 팁

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

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

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

라이브러리 임포트

시작하기 위해 필요한 라이브러리를 임포트합니다.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import patches

타원 매개변수 설정

이 단계에서는 타원의 매개변수를 설정합니다.

xcenter, ycenter = 0.38, 0.52
width, height = 0.1, 0.3
angle = -30

호 (Arc) 를 사용하여 타원 생성

이 단계에서는 호를 사용하여 타원을 생성합니다.

theta = np.deg2rad(np.arange(0.0, 360.0, 1.0))
x = 0.5 * width * np.cos(theta)
y = 0.5 * height * np.sin(theta)

rtheta = np.radians(angle)
R = np.array([
    [np.cos(rtheta), -np.sin(rtheta)],
    [np.sin(rtheta),  np.cos(rtheta)],
    ])

x, y = np.dot(R, [x, y])
x += xcenter
y += ycenter

다각형 근사 (Polygonal Approximation) 를 사용하여 타원 생성

이 단계에서는 다각형 근사를 사용하여 타원을 생성합니다.

theta = np.deg2rad(np.arange(0.0, 360.0, 1.0))
x = 0.5 * width * np.cos(theta)
y = 0.5 * height * np.sin(theta)

rtheta = np.radians(angle)
R = np.array([
    [np.cos(rtheta), -np.sin(rtheta)],
    [np.sin(rtheta),  np.cos(rtheta)],
    ])

x, y = np.dot(R, [x, y])
x += xcenter
y += ycenter

호 (Arc) 를 사용하여 타원 플롯 (Plot)

이 단계에서는 호를 사용하여 타원을 플롯합니다.

fig = plt.figure()
ax = fig.add_subplot(211, aspect='auto')
ax.fill(x, y, alpha=0.2, facecolor='yellow',
        edgecolor='yellow', linewidth=1, zorder=1)

e1 = patches.Arc((xcenter, ycenter), width, height,
                 angle=angle, linewidth=2, fill=False, zorder=2)

ax.add_patch(e1)

다각형 근사 (Polygonal Approximation) 를 사용하여 타원 플롯 (Plot)

이 단계에서는 다각형 근사를 사용하여 타원을 플롯합니다.

ax = fig.add_subplot(212, aspect='equal')
ax.fill(x, y, alpha=0.2, facecolor='green', edgecolor='green', zorder=1)
e2 = patches.Arc((xcenter, ycenter), width, height,
                 angle=angle, linewidth=2, fill=False, zorder=2)

ax.add_patch(e2)
fig.savefig('arc_compare')

plt.show()

요약

이 랩 (lab) 에서는 Python Matplotlib 을 사용하여 호 (arc) 로 생성된 타원과 다각형 근사 (polygonal approximation) 를 비교하는 방법을 배웠습니다. 타원 매개변수를 설정하고, 호와 다각형 근사를 사용하여 타원을 생성하고, Matplotlib 을 사용하여 이를 플롯하는 방법을 배웠습니다.