삼각 그리드에서 사각 그리드로의 보간

Beginner

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

소개

이 랩에서는 Python 의 Matplotlib 라이브러리를 사용하여 삼각 그리드 (triangular grid) 의 데이터를 사각 그리드 (quad grid) 로 보간하는 방법을 배우게 됩니다. 먼저 삼각 분할 (triangulation) 을 생성한 다음 선형 및 3 차 (cubic) 방법을 사용하여 데이터를 보간합니다. 마지막으로 결과를 플롯 (plot) 합니다.

VM 팁

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

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

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

삼각 분할 생성

첫 번째 단계는 주어진 x, y 및 삼각형 데이터를 사용하여 삼각 분할 (triangulation) 을 생성하는 것입니다. 그런 다음 삼각 분할을 플롯 (plot) 합니다.

## Create triangulation.
x = np.asarray([0, 1, 2, 3, 0.5, 1.5, 2.5, 1, 2, 1.5])
y = np.asarray([0, 0, 0, 0, 1.0, 1.0, 1.0, 2, 2, 3.0])
triangles = [[0, 1, 4], [1, 2, 5], [2, 3, 6], [1, 5, 4], [2, 6, 5], [4, 5, 7],
             [5, 6, 8], [5, 8, 7], [7, 8, 9]]
triang = mtri.Triangulation(x, y, triangles)

## Plot the triangulation.
plt.triplot(triang, 'ko-')
plt.title('Triangular grid')
plt.show()

선형 방법 (Linear Method) 을 사용하여 데이터 보간

두 번째 단계는 선형 방법 (linear method) 을 사용하여 데이터를 보간하는 것입니다. 정규 간격의 사각 그리드 (quad grid) 를 생성한 다음 LinearTriInterpolator 메서드를 사용하여 데이터를 보간합니다. 마지막으로 보간된 데이터를 플롯 (plot) 합니다.

## Interpolate to regularly-spaced quad grid.
z = np.cos(1.5 * x) * np.cos(1.5 * y)
xi, yi = np.meshgrid(np.linspace(0, 3, 20), np.linspace(0, 3, 20))

## Interpolate using linear method.
interp_lin = mtri.LinearTriInterpolator(triang, z)
zi_lin = interp_lin(xi, yi)

## Plot the interpolated data.
plt.contourf(xi, yi, zi_lin)
plt.plot(xi, yi, 'k-', lw=0.5, alpha=0.5)
plt.plot(xi.T, yi.T, 'k-', lw=0.5, alpha=0.5)
plt.title("Linear interpolation")
plt.show()

큐빅 방법 (Cubic Method) 을 사용하여 데이터 보간

세 번째 단계는 큐빅 방법 (cubic method) 을 사용하여 데이터를 보간하는 것입니다. kind 매개변수를 'geom' 또는 'min_E'로 설정하여 CubicTriInterpolator 메서드를 사용합니다. 마지막으로 보간된 데이터를 플롯 (plot) 합니다.

## Interpolate using cubic method with kind=geom.
interp_cubic_geom = mtri.CubicTriInterpolator(triang, z, kind='geom')
zi_cubic_geom = interp_cubic_geom(xi, yi)

## Plot the interpolated data.
plt.contourf(xi, yi, zi_cubic_geom)
plt.plot(xi, yi, 'k-', lw=0.5, alpha=0.5)
plt.plot(xi.T, yi.T, 'k-', lw=0.5, alpha=0.5)
plt.title("Cubic interpolation, kind='geom'")
plt.show()

## Interpolate using cubic method with kind=min_E.
interp_cubic_min_E = mtri.CubicTriInterpolator(triang, z, kind='min_E')
zi_cubic_min_E = interp_cubic_min_E(xi, yi)

## Plot the interpolated data.
plt.contourf(xi, yi, zi_cubic_min_E)
plt.plot(xi, yi, 'k-', lw=0.5, alpha=0.5)
plt.plot(xi.T, yi.T, 'k-', lw=0.5, alpha=0.5)
plt.title("Cubic interpolation, kind='min_E'")
plt.show()

요약

이 랩 (lab) 에서는 Python 의 Matplotlib 라이브러리를 사용하여 삼각 그리드 (triangular grid) 에서 사각 그리드 (quad grid) 로 데이터를 보간하는 방법을 배웠습니다. 삼각 분할 (triangulation) 을 생성하는 것으로 시작하여 선형 (linear) 및 큐빅 (cubic) 방법을 사용하여 데이터를 보간했습니다. 마지막으로 결과를 플롯 (plot) 했습니다.