.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/fractals_chaos/curves/plot_02_hilbert_curve.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_api_gallery_fractals_chaos_curves_plot_02_hilbert_curve.py: Hilbert's space-filling curve =================================== Draws the first few Hilbert curves, each visiting every cell of a finer grid. In the limit the curve passes through every point of the square while keeping nearby points of the curve close in the plane. .. GENERATED FROM PYTHON SOURCE LINES 11-15 .. code-block:: Python import matplotlib.pyplot as plt from mathematicskit.fractals_chaos import hilbert_curve .. GENERATED FROM PYTHON SOURCE LINES 16-18 Orders 1 to 5 ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 18-30 .. code-block:: Python fig, axes = plt.subplots(1, 5, figsize=(14, 3)) for ax, order in zip(axes, range(1, 6)): pts = hilbert_curve(order) n = 2**order ax.plot((pts[:, 0] + 0.5) / n, (pts[:, 1] + 0.5) / n, lw=1.2 if order < 4 else 0.6) ax.set_aspect("equal") ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.set_title(f"order {order}: {len(pts)} cells") ax.axis("off") .. image-sg:: /api/gallery/fractals_chaos/curves/images/sphx_glr_plot_02_hilbert_curve_001.png :alt: order 1: 4 cells, order 2: 16 cells, order 3: 64 cells, order 4: 256 cells, order 5: 1024 cells :srcset: /api/gallery/fractals_chaos/curves/images/sphx_glr_plot_02_hilbert_curve_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 31-33 Locality: nearby indices map to nearby cells ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-37 .. code-block:: Python pts = hilbert_curve(6) jumps = abs(pts[1:] - pts[:-1]).sum(axis=1) print(f"order 6: {len(pts)} cells, every step moves to a neighbouring cell: {bool((jumps == 1).all())}") .. rst-class:: sphx-glr-script-out .. code-block:: none order 6: 4096 cells, every step moves to a neighbouring cell: True .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.034 seconds) .. _sphx_glr_download_api_gallery_fractals_chaos_curves_plot_02_hilbert_curve.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_hilbert_curve.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_hilbert_curve.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_hilbert_curve.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_