.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/mplot3d/lorenz_attractor.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. 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_gallery_mplot3d_lorenz_attractor.py: ================ Lorenz attractor ================ This is an example of plotting Edward Lorenz's 1963 `"Deterministic Nonperiodic Flow"`_ in a 3-dimensional space using mplot3d. .. _"Deterministic Nonperiodic Flow": https://journals.ametsoc.org/view/journals/atsc/20/2/1520-0469_1963_020_0130_dnf_2_0_co_2.xml .. note:: Because this is a simple non-linear ODE, it would be more easily done using SciPy's ODE solver, but this approach depends only upon NumPy. .. GENERATED FROM PYTHON SOURCE LINES 16-62 .. image-sg:: /gallery/mplot3d/images/sphx_glr_lorenz_attractor_001.png :alt: Lorenz Attractor :srcset: /gallery/mplot3d/images/sphx_glr_lorenz_attractor_001.png, /gallery/mplot3d/images/sphx_glr_lorenz_attractor_001_2_00x.png 2.00x :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import numpy as np def lorenz(xyz, *, s=10, r=28, b=2.667): """ Parameters ---------- xyz : array-like, shape (3,) Point of interest in three-dimensional space. s, r, b : float Parameters defining the Lorenz attractor. Returns ------- xyz_dot : array, shape (3,) Values of the Lorenz attractor's partial derivatives at *xyz*. """ x, y, z = xyz x_dot = s*(y - x) y_dot = r*x - y - x*z z_dot = x*y - b*z return np.array([x_dot, y_dot, z_dot]) dt = 0.01 num_steps = 10000 xyzs = np.empty((num_steps + 1, 3)) # Need one more for the initial values xyzs[0] = (0., 1., 1.05) # Set initial values # Step through "time", calculating the partial derivatives at the current point # and using them to estimate the next point for i in range(num_steps): xyzs[i + 1] = xyzs[i] + lorenz(xyzs[i]) * dt # Plot ax = plt.figure().add_subplot(projection='3d') ax.plot(*xyzs.T, lw=0.5) ax.set_xlabel("X Axis") ax.set_ylabel("Y Axis") ax.set_zlabel("Z Axis") ax.set_title("Lorenz Attractor") plt.show() .. _sphx_glr_download_gallery_mplot3d_lorenz_attractor.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: lorenz_attractor.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: lorenz_attractor.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_