.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/fractals_chaos/ifs/plot_02_barnsley_fern.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_ifs_plot_02_barnsley_fern.py: Barnsley's fern: an iterated function system ================================================== Barnsley's fern is the attractor of just four contractive affine maps, drawn with the chaos game: start anywhere, and at each step apply one of the maps chosen at random with Barnsley's probabilities. Hutchinson's theorem says the fern is the unique set equal to the union of its four images under the maps, which the second figure shows directly. .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.fractals_chaos import BarnsleyFern from mathematicskit.fractals_chaos.visualizers.plots import plot_ifs_points .. GENERATED FROM PYTHON SOURCE LINES 20-22 The fern from the chaos game ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-31 .. code-block:: Python fern = BarnsleyFern() points = fern.generate(60000, seed=0) ax = plot_ifs_points(points, color="darkgreen") ax.set_title("Barnsley fern (60 000 chaos-game points)") for name, (A, _, p) in zip(["stem", "main copy", "left leaflet", "right leaflet"], fern.transforms): print(f"{name:14s} p = {p:.2f}, contraction |det A| = {abs(np.linalg.det(A)):.3f}") .. image-sg:: /api/gallery/fractals_chaos/ifs/images/sphx_glr_plot_02_barnsley_fern_001.png :alt: Barnsley fern (60 000 chaos-game points) :srcset: /api/gallery/fractals_chaos/ifs/images/sphx_glr_plot_02_barnsley_fern_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none stem p = 0.01, contraction |det A| = 0.000 main copy p = 0.85, contraction |det A| = 0.724 left leaflet p = 0.07, contraction |det A| = 0.104 right leaflet p = 0.07, contraction |det A| = 0.109 .. GENERATED FROM PYTHON SOURCE LINES 32-38 The fern is four smaller ferns ------------------------------ Applying each map to the whole fern gives four pieces -- the stem, the fern minus its lowest leaflets, and the two lowest leaflets -- whose union is the fern again. .. GENERATED FROM PYTHON SOURCE LINES 38-46 .. code-block:: Python fig, ax = plt.subplots(figsize=(5, 8)) colors = ["saddlebrown", "darkgreen", "royalblue", "darkorange"] for (A, b, _), color in zip(fern.transforms, colors): image = points @ A.T + b ax.scatter(image[:, 0], image[:, 1], s=0.2, color=color, linewidths=0) ax.set_aspect("equal") ax.set_title("Images of the fern under its four maps") .. image-sg:: /api/gallery/fractals_chaos/ifs/images/sphx_glr_plot_02_barnsley_fern_002.png :alt: Images of the fern under its four maps :srcset: /api/gallery/fractals_chaos/ifs/images/sphx_glr_plot_02_barnsley_fern_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Images of the fern under its four maps') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.090 seconds) .. _sphx_glr_download_api_gallery_fractals_chaos_ifs_plot_02_barnsley_fern.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_barnsley_fern.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_barnsley_fern.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_barnsley_fern.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_