.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/gridding/Find_Natural_Neighbors_Verification.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_gridding_Find_Natural_Neighbors_Verification.py: =================================== Find Natural Neighbors Verification =================================== Finding natural neighbors in a triangulation A triangle is a natural neighbor of a point if that point is within a circumscribed circle ("circumcircle") containing the triangle. .. GENERATED FROM PYTHON SOURCE LINES 14-38 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.spatial import Delaunay from metpy.interpolate.geometry import circumcircle_radius, find_natural_neighbors # Create test observations, test points, and plot the triangulation and points. gx, gy = np.meshgrid(np.arange(0, 20, 4), np.arange(0, 20, 4)) pts = np.vstack([gx.ravel(), gy.ravel()]).T tri = Delaunay(pts) fig, ax = plt.subplots(figsize=(15, 10)) for i, inds in enumerate(tri.simplices): pts = tri.points[inds] x, y = np.vstack((pts, pts[0])).T ax.plot(x, y) ax.annotate(i, xy=(np.mean(x), np.mean(y))) test_points = np.array([[2, 2], [5, 10], [12, 13.4], [12, 8], [20, 20]]) for i, (x, y) in enumerate(test_points): ax.plot(x, y, 'k.', markersize=6) ax.annotate('test ' + str(i), xy=(x, y)) .. image-sg:: /examples/gridding/images/sphx_glr_Find_Natural_Neighbors_Verification_001.png :alt: Find Natural Neighbors Verification :srcset: /examples/gridding/images/sphx_glr_Find_Natural_Neighbors_Verification_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 39-47 Since finding natural neighbors already calculates circumcenters, return that information for later use. The key of the neighbors dictionary refers to the test point index, and the list of integers are the triangles that are natural neighbors of that particular test point. Since point 4 is far away from the triangulation, it has no natural neighbors. Point 3 is at the confluence of several triangles so it has many natural neighbors. .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: Python neighbors, circumcenters = find_natural_neighbors(tri, test_points) print(neighbors) .. rst-class:: sphx-glr-script-out .. code-block:: none {0: [0, 1], 1: [18, 19], 2: [16, 17, 22, 23], 3: [24, 25, 26, 27, 28, 29, 30, 31], 4: []} .. GENERATED FROM PYTHON SOURCE LINES 51-53 We can plot all of the triangles as well as the circles representing the circumcircles .. GENERATED FROM PYTHON SOURCE LINES 53-70 .. code-block:: Python fig, ax = plt.subplots(figsize=(15, 10)) for i, inds in enumerate(tri.simplices): pts = tri.points[inds] x, y = np.vstack((pts, pts[0])).T ax.plot(x, y) ax.annotate(i, xy=(np.mean(x), np.mean(y))) # Using circumcenters and calculated circumradii, plot the circumcircles for idx, cc in enumerate(circumcenters): ax.plot(cc[0], cc[1], 'k.', markersize=5) circ = plt.Circle(cc, circumcircle_radius(*tri.points[tri.simplices[idx]]), edgecolor='k', facecolor='none', transform=fig.axes[0].transData) ax.add_artist(circ) ax.set_aspect('equal', 'datalim') plt.show() .. image-sg:: /examples/gridding/images/sphx_glr_Find_Natural_Neighbors_Verification_002.png :alt: Find Natural Neighbors Verification :srcset: /examples/gridding/images/sphx_glr_Find_Natural_Neighbors_Verification_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.351 seconds) .. _sphx_glr_download_examples_gridding_Find_Natural_Neighbors_Verification.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: Find_Natural_Neighbors_Verification.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: Find_Natural_Neighbors_Verification.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_