.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/2d-lsq.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_auto_examples_2d-lsq.py: =============================== 2-Dimensional Least Squares Fit =============================== .. GENERATED FROM PYTHON SOURCE LINES 6-61 .. image-sg:: /auto_examples/images/sphx_glr_2d-lsq_001.png :alt: 2d lsq :srcset: /auto_examples/images/sphx_glr_2d-lsq_001.png :class: sphx-glr-single-img .. code-block:: Python :lineno-start: 7 import ndsplines import matplotlib.pyplot as plt import numpy as np NUM_X = 50 NUM_Y = 50 x = np.linspace(-3, 3, NUM_X) y = np.linspace(-3, 3, NUM_Y) meshx, meshy = np.meshgrid(x, y, indexing="ij") input_coords = np.stack((meshx, meshy), axis=-1) K = np.array( [ [ 1, -0.7, ], [-0.7, 1.5], ] ) meshz = np.exp( -np.einsum( K, [ 1, 2, ], input_coords, [..., 1], input_coords, [..., 2], ) ) + 0.1 * np.random.randn(NUM_X, NUM_Y) xt = [-1, 0, 1] yt = [-1, 0, 1] k = 3 xt = np.r_[(x[0],) * (k + 1), xt, (x[-1],) * (k + 1)] yt = np.r_[(y[0],) * (k + 1), yt, (y[-1],) * (k + 1)] ts = [xt, yt] samplex = input_coords.reshape((-1, 2)) sampley = meshz.reshape((-1)) spl = ndsplines.make_lsq_spline(samplex, sampley, ts, np.array([3, 3])) fig = plt.figure() ax = fig.add_subplot(111, projection="3d") ax.scatter(meshx, meshy, meshz, alpha=0.25) ax.plot_wireframe(meshx, meshy, spl(input_coords), color="C1") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.118 seconds) .. _sphx_glr_download_auto_examples_2d-lsq.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 2d-lsq.ipynb <2d-lsq.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 2d-lsq.py <2d-lsq.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 2d-lsq.zip <2d-lsq.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_