.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "example_gallery/vector_synergy/interpolate_from_points.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_example_gallery_vector_synergy_interpolate_from_points.py: Interpolate from points ======================= Fill a BoundedGrid based on point data Introduction ------------ In this example a BoundedGrid is created from a set of points by interpolating between the points. For this operation we need: #. locations of the points #. values of the points #. a grid to interpolate on In this example, we simulate the point data using a 2d sinusoidal equation. We sample the equation at pseudo-random locations. .. GENERATED FROM PYTHON SOURCE LINES 21-32 .. code-block:: Python import numpy numpy.random.seed(0) x = 100 * numpy.random.rand(100) numpy.random.seed(1) y = 100 * numpy.random.rand(100) values = numpy.sin(x / (10 * numpy.pi)) * numpy.sin(y / (10 * numpy.pi)) points = numpy.array([x, y]).T .. GENERATED FROM PYTHON SOURCE LINES 34-35 This input data looks as follows: .. GENERATED FROM PYTHON SOURCE LINES 36-44 .. code-block:: Python import matplotlib.pyplot as plt plt.scatter(x, y, c=values) plt.colorbar() plt.title("Simulated input values") plt.show() .. image-sg:: /example_gallery/vector_synergy/images/sphx_glr_interpolate_from_points_001.png :alt: Simulated input values :srcset: /example_gallery/vector_synergy/images/sphx_glr_interpolate_from_points_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 45-46 Now we can create a grid and interpolate onto that grid. .. GENERATED FROM PYTHON SOURCE LINES 47-55 .. code-block:: Python from gridkit import HexGrid empty_grid = HexGrid(size=6, shape="pointy") data_grid = empty_grid.interp_from_points(points, values) from matplotlib.patches import Rectangle .. GENERATED FROM PYTHON SOURCE LINES 56-59 Currently the interpolation methods "nearest", "linear" and "cubic" are supported, based on scpiy's ``NearestNDInterpolator``, ``LinearNDInterpolator`` and ``CloughTocher2DInterpolator``, respectively. Here we try each method and plot them next to each other to compare. .. GENERATED FROM PYTHON SOURCE LINES 60-94 .. code-block:: Python from gridkit.doc_utils import plot_polygons fig, axes = plt.subplots(1, 3, sharey=True, figsize=(12, 5)) for ax, method in zip(axes, ("nearest", "linear", "cubic")): data_grid = empty_grid.interp_from_points( points, values, method=method, ) # plot the interpolated values at the cells plot_polygons(data_grid.to_shapely(), data_grid.data.ravel(), "viridis", ax=ax) # plot original data ax.scatter(x, y, c=values, edgecolors="black") # add outline of the grid bounds b = data_grid.bounds rect = Rectangle( (b[0], b[1]), b[2] - b[0], b[3] - b[1], linewidth=1, edgecolor="r", facecolor="none", ) ax.add_patch(rect) ax.set_title(method, fontdict={"fontsize": 30}) fig.tight_layout() plt.show() .. image-sg:: /example_gallery/vector_synergy/images/sphx_glr_interpolate_from_points_002.png :alt: nearest, linear, cubic :srcset: /example_gallery/vector_synergy/images/sphx_glr_interpolate_from_points_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 95-100 In this example, the `cubic` interpolation represents the original function the best, but this will differ per usecase. Note how the BoundedGrid has nodata values at the border in the "linear" and "cubic" cases. The bounds of the grid are automatically chosen to align with the supplied grid and accommodate all points. Cells of which the center is not within the convex hull of the data, but are within the selected bounds, get a `nodata_value` as specified in ``interp_from_points`` (default numpy.nan). .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.507 seconds) .. _sphx_glr_download_example_gallery_vector_synergy_interpolate_from_points.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: interpolate_from_points.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: interpolate_from_points.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: interpolate_from_points.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_