
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "_auto_examples/plot_example1.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download__auto_examples_plot_example1.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr__auto_examples_plot_example1.py:


Closed interpolating splines
============================

This example demonstrates how the choice of basis function influences the shape of a spline. We use a fixed set of knots and construct splines using different basis functions to observe their effects.

.. GENERATED FROM PYTHON SOURCE LINES 7-13

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np
    import splinebox.basis_functions
    import splinebox.spline_curves








.. GENERATED FROM PYTHON SOURCE LINES 14-15

We define a set of arbitrary 2D points as knots.

.. GENERATED FROM PYTHON SOURCE LINES 15-18

.. code-block:: Python


    knots = np.array([[0, 100], [50, 100], [100, 50], [100, 0], [100, -100], [50, -50], [0, -100], [-100, 0]])








.. GENERATED FROM PYTHON SOURCE LINES 19-20

We define the parameter values to evaluate the spline for plotting.

.. GENERATED FROM PYTHON SOURCE LINES 20-23

.. code-block:: Python


    t = np.linspace(0, len(knots), 1000)








.. GENERATED FROM PYTHON SOURCE LINES 24-26

Set up a grid of subplots for visual comparison of different basis functions
and loop through different basis functions to construct the corresponding splines and plot them.

.. GENERATED FROM PYTHON SOURCE LINES 26-49

.. code-block:: Python


    n, m = 2, 2
    fig, axes = plt.subplots(n, m, sharex=True, sharey=True)

    for i, (name, basis_function) in enumerate(
        (
            ("B1", splinebox.basis_functions.B1()),
            ("B3", splinebox.basis_functions.B3()),
            ("Exponential", splinebox.basis_functions.Exponential(len(knots))),
            ("CatmullRom", splinebox.basis_functions.CatmullRom()),
        )
    ):
        M = len(knots)
        curve = splinebox.spline_curves.Spline(M, basis_function, True)
        curve.knots = knots
        discreteContour = curve(t)

        axes[i // n, i % n].scatter(knots[:, 0], knots[:, 1])
        axes[i // n, i % n].plot(discreteContour[:, 0], discreteContour[:, 1])
        axes[i // n, i % n].set_title(name)
        axes[i // n, i % n].set_aspect("equal", adjustable="box")

    plt.show()



.. image-sg:: /_auto_examples/images/sphx_glr_plot_example1_001.png
   :alt: B1, B3, Exponential, CatmullRom
   :srcset: /_auto_examples/images/sphx_glr_plot_example1_001.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.834 seconds)


.. _sphx_glr_download__auto_examples_plot_example1.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_example1.ipynb <plot_example1.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_example1.py <plot_example1.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_example1.zip <plot_example1.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
