.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/fractals_chaos/lsystems/plot_01_lindenmayer_plants.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_lsystems_plot_01_lindenmayer_plants.py: Lindenmayer systems: from algae to plants =============================================== Runs Lindenmayer's 1968 algae system, whose string lengths are Fibonacci numbers, and draws a Koch curve and a branching plant with turtle graphics. .. GENERATED FROM PYTHON SOURCE LINES 11-15 .. code-block:: Python import matplotlib.pyplot as plt from mathematicskit.fractals_chaos import lsystem, turtle_path .. GENERATED FROM PYTHON SOURCE LINES 16-18 Lindenmayer's algae ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 18-23 .. code-block:: Python for n in range(7): word = lsystem("A", {"A": "AB", "B": "A"}, n) print(f"n = {n}: {word} (length {len(word)})") .. rst-class:: sphx-glr-script-out .. code-block:: none n = 0: A (length 1) n = 1: AB (length 2) n = 2: ABA (length 3) n = 3: ABAAB (length 5) n = 4: ABAABABA (length 8) n = 5: ABAABABAABAAB (length 13) n = 6: ABAABABAABAABABAABABA (length 21) .. GENERATED FROM PYTHON SOURCE LINES 24-26 A Koch curve and a fractal plant ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 26-37 .. code-block:: Python koch = turtle_path(lsystem("F", {"F": "F+F--F+F"}, 4), 60, heading=0) plant = turtle_path(lsystem("X", {"X": "F+[[X]-X]-F[-FX]+X", "F": "FF"}, 5), 25, heading=65) fig, axes = plt.subplots(1, 2, figsize=(10, 5)) for ax, lines, title in zip(axes, (koch, plant), ("Koch curve: F -> F+F--F+F", "plant: X -> F+[[X]-X]-F[-FX]+X")): for line in lines: ax.plot(*line.T, color="darkgreen" if ax is axes[1] else "tab:blue", lw=0.6) ax.set_aspect("equal") ax.axis("off") ax.set_title(title, fontsize=10) .. image-sg:: /api/gallery/fractals_chaos/lsystems/images/sphx_glr_plot_01_lindenmayer_plants_001.png :alt: Koch curve: F -> F+F--F+F, plant: X -> F+[[X]-X]-F[-FX]+X :srcset: /api/gallery/fractals_chaos/lsystems/images/sphx_glr_plot_01_lindenmayer_plants_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.070 seconds) .. _sphx_glr_download_api_gallery_fractals_chaos_lsystems_plot_01_lindenmayer_plants.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_lindenmayer_plants.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_lindenmayer_plants.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_lindenmayer_plants.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_