Triinterp 演示

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

  1. import matplotlib.pyplot as plt
  2. import matplotlib.tri as mtri
  3. import numpy as np
  4. # Create triangulation.
  5. x = np.asarray([0, 1, 2, 3, 0.5, 1.5, 2.5, 1, 2, 1.5])
  6. y = np.asarray([0, 0, 0, 0, 1.0, 1.0, 1.0, 2, 2, 3.0])
  7. triangles = [[0, 1, 4], [1, 2, 5], [2, 3, 6], [1, 5, 4], [2, 6, 5], [4, 5, 7],
  8. [5, 6, 8], [5, 8, 7], [7, 8, 9]]
  9. triang = mtri.Triangulation(x, y, triangles)
  10. # Interpolate to regularly-spaced quad grid.
  11. z = np.cos(1.5 * x) * np.cos(1.5 * y)
  12. xi, yi = np.meshgrid(np.linspace(0, 3, 20), np.linspace(0, 3, 20))
  13. interp_lin = mtri.LinearTriInterpolator(triang, z)
  14. zi_lin = interp_lin(xi, yi)
  15. interp_cubic_geom = mtri.CubicTriInterpolator(triang, z, kind='geom')
  16. zi_cubic_geom = interp_cubic_geom(xi, yi)
  17. interp_cubic_min_E = mtri.CubicTriInterpolator(triang, z, kind='min_E')
  18. zi_cubic_min_E = interp_cubic_min_E(xi, yi)
  19. # Set up the figure
  20. fig, axs = plt.subplots(nrows=2, ncols=2)
  21. axs = axs.flatten()
  22. # Plot the triangulation.
  23. axs[0].tricontourf(triang, z)
  24. axs[0].triplot(triang, 'ko-')
  25. axs[0].set_title('Triangular grid')
  26. # Plot linear interpolation to quad grid.
  27. axs[1].contourf(xi, yi, zi_lin)
  28. axs[1].plot(xi, yi, 'k-', lw=0.5, alpha=0.5)
  29. axs[1].plot(xi.T, yi.T, 'k-', lw=0.5, alpha=0.5)
  30. axs[1].set_title("Linear interpolation")
  31. # Plot cubic interpolation to quad grid, kind=geom
  32. axs[2].contourf(xi, yi, zi_cubic_geom)
  33. axs[2].plot(xi, yi, 'k-', lw=0.5, alpha=0.5)
  34. axs[2].plot(xi.T, yi.T, 'k-', lw=0.5, alpha=0.5)
  35. axs[2].set_title("Cubic interpolation,\nkind='geom'")
  36. # Plot cubic interpolation to quad grid, kind=min_E
  37. axs[3].contourf(xi, yi, zi_cubic_min_E)
  38. axs[3].plot(xi, yi, 'k-', lw=0.5, alpha=0.5)
  39. axs[3].plot(xi.T, yi.T, 'k-', lw=0.5, alpha=0.5)
  40. axs[3].set_title("Cubic interpolation,\nkind='min_E'")
  41. fig.tight_layout()
  42. plt.show()

Triinterp 演示

参考

此示例中显示了以下函数,方法,类和模块的使用:

  1. import matplotlib
  2. matplotlib.axes.Axes.tricontourf
  3. matplotlib.pyplot.tricontourf
  4. matplotlib.axes.Axes.triplot
  5. matplotlib.pyplot.triplot
  6. matplotlib.axes.Axes.contourf
  7. matplotlib.pyplot.contourf
  8. matplotlib.axes.Axes.plot
  9. matplotlib.pyplot.plot
  10. matplotlib.tri
  11. matplotlib.tri.LinearTriInterpolator
  12. matplotlib.tri.CubicTriInterpolator
  13. matplotlib.tri.Triangulation

下载这个示例