从三角网格到四边形网格的插值

MatplotlibMatplotlibBeginner
立即练习

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

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本实验中,你将学习如何使用 Python 的 Matplotlib 库将三角网格中的数据插值到四边形网格。我们将首先创建一个三角剖分,然后使用线性和三次方法对数据进行插值。最后,我们将绘制结果。

虚拟机使用提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签页,以访问 Jupyter Notebook 进行练习。

有时,你可能需要等待几秒钟让 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时向 Labby 提问。课程结束后提供反馈,我们将立即为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlotCustomizationGroup(["Plot Customization"]) matplotlib(("Matplotlib")) -.-> matplotlib/SpecializedPlotsGroup(["Specialized Plots"]) python(("Python")) -.-> python/FileHandlingGroup(["File Handling"]) 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 和三角形数据创建一个三角剖分。然后我们将绘制该三角剖分。

## 创建三角剖分。
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)

## 绘制三角剖分。
plt.triplot(triang, 'ko-')
plt.title('Triangular grid')
plt.show()

使用线性方法插值数据

第二步是使用线性方法对数据进行插值。我们将创建一个规则间隔的四边形网格,然后使用线性三角形插值器(LinearTriInterpolator)方法对数据进行插值。最后,我们将绘制插值后的数据。

## 插值到规则间隔的四边形网格。
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))

## 使用线性方法插值。
interp_lin = mtri.LinearTriInterpolator(triang, z)
zi_lin = interp_lin(xi, yi)

## 绘制插值后的数据。
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("线性插值")
plt.show()

使用三次方法插值数据

第三步是使用三次方法对数据进行插值。我们将使用三次三角形插值器(CubicTriInterpolator)方法,并将 kind 参数设置为 'geom' 或'min_E'。最后,我们将绘制插值后的数据。

## 使用 kind='geom' 的三次方法进行插值。
interp_cubic_geom = mtri.CubicTriInterpolator(triang, z, kind='geom')
zi_cubic_geom = interp_cubic_geom(xi, yi)

## 绘制插值后的数据。
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("三次插值,kind='geom'")
plt.show()

## 使用 kind='min_E' 的三次方法进行插值。
interp_cubic_min_E = mtri.CubicTriInterpolator(triang, z, kind='min_E')
zi_cubic_min_E = interp_cubic_min_E(xi, yi)

## 绘制插值后的数据。
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("三次插值,kind='min_E'")
plt.show()

总结

在本实验中,我们学习了如何使用 Python 的 Matplotlib 库将三角网格中的数据插值到四边形网格。我们首先创建了一个三角剖分,然后使用线性和三次方法对数据进行插值。最后,我们绘制了结果。