.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/kneighbors_gridder.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_kneighbors_gridder.py: Gridding with a nearest-neighbors interpolator ============================================== Verde offers the :class:`verde.KNeighbors` class for nearest-neighbor gridding. The interpolation looks at the data values of the *k* nearest neighbors of a interpolated point. If *k* is 1, then the data value of the closest neighbor is assigned to the point. If *k* is greater than 1, the average value of the closest *k* neighbors is assigned to the point. The interpolation works on Cartesian data, so if we want to grid geographic data (like our Baja California bathymetry) we need to project them into a Cartesian system. We'll use `pyproj `__ to calculate a Mercator projection for the data. For convenience, Verde still allows us to make geographic grids by passing the ``projection`` argument to :meth:`verde.KNeighbors.grid` and the like. When doing so, the grid will be generated using geographic coordinates which will be projected prior to interpolation. .. GENERATED FROM PYTHON SOURCE LINES 27-93 .. image-sg:: /gallery/images/sphx_glr_kneighbors_gridder_001.png :alt: Nearest-neighbor gridding of bathymetry :srcset: /gallery/images/sphx_glr_kneighbors_gridder_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Data region: (245.0, 254.705, 20.0, 29.99131) Generated geographic grid: Dimensions: (latitude: 600, longitude: 583) Coordinates: * longitude (longitude) float64 245.0 245.0 245.0 ... 254.7 254.7 254.7 * latitude (latitude) float64 20.0 20.02 20.03 ... 29.96 29.97 29.99 Data variables: bathymetry_m (latitude, longitude) float64 -3.669e+03 -3.669e+03 ... -66.5 Attributes: metadata: Generated by KNeighbors(k=10, reduction=` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: kneighbors_gridder.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_