Tricontour 德洛内三角

演示一组随机点的高分辨率三视图;matplotlib.tri.TriAnalyzer用于提高绘图质量。

该演示的初始数据点和三角形网格如下:

  • 在[-1, 1] x [-1, 1] 正方形内实例化一组随机点。
  • 然后计算这些点的Delaunay三角剖分,其中一个随机三角形子集由用户隐藏(基于init_mASK_frac参数)。这将模拟无效数据。

为获得这类数据集的高分辨率轮廓而提出的通用程序如下:

  1. 使用matplotlib.tri.TriAnalyzer计算扩展掩码,该掩码将从三角剖分的边框中排除形状不佳(平坦)的三角形。将掩码应用于三角剖分(使用SET_MASK)。
  2. 使用matplotlib.tri.UniformTriRefiner对数据进行细化和插值。
  3. tricontour绘制精确的数据。
  1. from matplotlib.tri import Triangulation, TriAnalyzer, UniformTriRefiner
  2. import matplotlib.pyplot as plt
  3. import matplotlib.cm as cm
  4. import numpy as np
  5. #-----------------------------------------------------------------------------
  6. # Analytical test function
  7. #-----------------------------------------------------------------------------
  8. def experiment_res(x, y):
  9. """ An analytic function representing experiment results """
  10. x = 2. * x
  11. r1 = np.sqrt((0.5 - x)**2 + (0.5 - y)**2)
  12. theta1 = np.arctan2(0.5 - x, 0.5 - y)
  13. r2 = np.sqrt((-x - 0.2)**2 + (-y - 0.2)**2)
  14. theta2 = np.arctan2(-x - 0.2, -y - 0.2)
  15. z = (4 * (np.exp((r1 / 10)**2) - 1) * 30. * np.cos(3 * theta1) +
  16. (np.exp((r2 / 10)**2) - 1) * 30. * np.cos(5 * theta2) +
  17. 2 * (x**2 + y**2))
  18. return (np.max(z) - z) / (np.max(z) - np.min(z))
  19. #-----------------------------------------------------------------------------
  20. # Generating the initial data test points and triangulation for the demo
  21. #-----------------------------------------------------------------------------
  22. # User parameters for data test points
  23. n_test = 200 # Number of test data points, tested from 3 to 5000 for subdiv=3
  24. subdiv = 3 # Number of recursive subdivisions of the initial mesh for smooth
  25. # plots. Values >3 might result in a very high number of triangles
  26. # for the refine mesh: new triangles numbering = (4**subdiv)*ntri
  27. init_mask_frac = 0.0 # Float > 0. adjusting the proportion of
  28. # (invalid) initial triangles which will be masked
  29. # out. Enter 0 for no mask.
  30. min_circle_ratio = .01 # Minimum circle ratio - border triangles with circle
  31. # ratio below this will be masked if they touch a
  32. # border. Suggested value 0.01; use -1 to keep
  33. # all triangles.
  34. # Random points
  35. random_gen = np.random.RandomState(seed=19680801)
  36. x_test = random_gen.uniform(-1., 1., size=n_test)
  37. y_test = random_gen.uniform(-1., 1., size=n_test)
  38. z_test = experiment_res(x_test, y_test)
  39. # meshing with Delaunay triangulation
  40. tri = Triangulation(x_test, y_test)
  41. ntri = tri.triangles.shape[0]
  42. # Some invalid data are masked out
  43. mask_init = np.zeros(ntri, dtype=bool)
  44. masked_tri = random_gen.randint(0, ntri, int(ntri * init_mask_frac))
  45. mask_init[masked_tri] = True
  46. tri.set_mask(mask_init)
  47. #-----------------------------------------------------------------------------
  48. # Improving the triangulation before high-res plots: removing flat triangles
  49. #-----------------------------------------------------------------------------
  50. # masking badly shaped triangles at the border of the triangular mesh.
  51. mask = TriAnalyzer(tri).get_flat_tri_mask(min_circle_ratio)
  52. tri.set_mask(mask)
  53. # refining the data
  54. refiner = UniformTriRefiner(tri)
  55. tri_refi, z_test_refi = refiner.refine_field(z_test, subdiv=subdiv)
  56. # analytical 'results' for comparison
  57. z_expected = experiment_res(tri_refi.x, tri_refi.y)
  58. # for the demo: loading the 'flat' triangles for plot
  59. flat_tri = Triangulation(x_test, y_test)
  60. flat_tri.set_mask(~mask)
  61. #-----------------------------------------------------------------------------
  62. # Now the plots
  63. #-----------------------------------------------------------------------------
  64. # User options for plots
  65. plot_tri = True # plot of base triangulation
  66. plot_masked_tri = True # plot of excessively flat excluded triangles
  67. plot_refi_tri = False # plot of refined triangulation
  68. plot_expected = False # plot of analytical function values for comparison
  69. # Graphical options for tricontouring
  70. levels = np.arange(0., 1., 0.025)
  71. cmap = cm.get_cmap(name='Blues', lut=None)
  72. fig, ax = plt.subplots()
  73. ax.set_aspect('equal')
  74. ax.set_title("Filtering a Delaunay mesh\n" +
  75. "(application to high-resolution tricontouring)")
  76. # 1) plot of the refined (computed) data contours:
  77. ax.tricontour(tri_refi, z_test_refi, levels=levels, cmap=cmap,
  78. linewidths=[2.0, 0.5, 1.0, 0.5])
  79. # 2) plot of the expected (analytical) data contours (dashed):
  80. if plot_expected:
  81. ax.tricontour(tri_refi, z_expected, levels=levels, cmap=cmap,
  82. linestyles='--')
  83. # 3) plot of the fine mesh on which interpolation was done:
  84. if plot_refi_tri:
  85. ax.triplot(tri_refi, color='0.97')
  86. # 4) plot of the initial 'coarse' mesh:
  87. if plot_tri:
  88. ax.triplot(tri, color='0.7')
  89. # 4) plot of the unvalidated triangles from naive Delaunay Triangulation:
  90. if plot_masked_tri:
  91. ax.triplot(flat_tri, color='red')
  92. plt.show()

Tricontour 德洛内三角

参考

本例中显示了下列函数、方法、类和模块的使用:

  1. import matplotlib
  2. matplotlib.axes.Axes.tricontour
  3. matplotlib.pyplot.tricontour
  4. matplotlib.axes.Axes.tricontourf
  5. matplotlib.pyplot.tricontourf
  6. matplotlib.axes.Axes.triplot
  7. matplotlib.pyplot.triplot
  8. matplotlib.tri
  9. matplotlib.tri.Triangulation
  10. matplotlib.tri.TriAnalyzer
  11. matplotlib.tri.UniformTriRefiner

下载这个示例