三角グリッドから四角グリッドへの補間

MatplotlibMatplotlibBeginner
今すぐ練習

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

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、PythonのMatplotlibライブラリを使って、三角グリッドからのデータを四角グリッドに補間する方法を学びます。まず三角分割を作成し、その後線形および3次の方法を使ってデータを補間します。最後に結果をプロットします。

VMのヒント

VMの起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebookを使って練習しましょう。

時々、Jupyter Notebookが読み込み終わるまで数秒待つ必要がある場合があります。Jupyter Notebookの制限により、操作の検証を自動化することはできません。

学習中に問題に遭遇した場合は、Labbyにお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("Matplotlib")) -.-> matplotlib/PlotCustomizationGroup(["Plot Customization"]) matplotlib(("Matplotlib")) -.-> matplotlib/SpecializedPlotsGroup(["Specialized Plots"]) python(("Python")) -.-> python/FileHandlingGroup(["File Handling"]) python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python/BasicConceptsGroup -.-> python/comments("Comments") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("Line Plots") python/DataStructuresGroup -.-> python/lists("Lists") python/DataStructuresGroup -.-> python/tuples("Tuples") matplotlib/PlotCustomizationGroup -.-> matplotlib/titles_labels("Adding Titles and Labels") matplotlib/SpecializedPlotsGroup -.-> matplotlib/contour_plots("Contour Plots") python/FileHandlingGroup -.-> python/with_statement("Using with Statement") subgraph Lab Skills python/comments -.-> lab-49009{{"三角グリッドから四角グリッドへの補間"}} matplotlib/line_plots -.-> lab-49009{{"三角グリッドから四角グリッドへの補間"}} python/lists -.-> lab-49009{{"三角グリッドから四角グリッドへの補間"}} python/tuples -.-> lab-49009{{"三角グリッドから四角グリッドへの補間"}} matplotlib/titles_labels -.-> lab-49009{{"三角グリッドから四角グリッドへの補間"}} matplotlib/contour_plots -.-> lab-49009{{"三角グリッドから四角グリッドへの補間"}} python/with_statement -.-> lab-49009{{"三角グリッドから四角グリッドへの補間"}} end

三角分割を作成する

最初のステップは、与えられたx、y、および三角形のデータを使って三角分割を作成することです。その後、三角分割をプロットします。

## 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()

線形補間によるデータの補間

2番目のステップは、線形補間法を使ってデータを補間することです。等間隔の四角グリッドを作成し、その後、LinearTriInterpolatorメソッドを使ってデータを補間します。最後に、補間されたデータをプロットします。

## 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()

3次補間によるデータの補間

3番目のステップは、3次補間法を使ってデータを補間することです。kindパラメータを'geom'または'min_E'に設定したCubicTriInterpolatorメソッドを使います。最後に、補間されたデータをプロットします。

## 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()

まとめ

この実験では、PythonのMatplotlibライブラリを使って、三角グリッドからのデータを四角グリッドに補間する方法を学びました。まず三角分割を作成し、その後線形および3次の方法を使ってデータを補間しました。最後に、結果をプロットしました。