Plotting Lorenz Attractor in 3D using Python

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab is a step-by-step tutorial for plotting Edward Lorenz's 1963 "Deterministic Nonperiodic Flow" in a 3-dimensional space using mplot3d. We will use Python and Matplotlib, which is a plotting library for the Python programming language.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} matplotlib/importing_matplotlib -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} matplotlib/figures_axes -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} matplotlib/line_plots -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} python/variables_data_types -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} python/numeric_types -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} python/for_loops -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} python/lists -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} python/tuples -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} python/function_definition -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} python/default_arguments -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} python/importing_modules -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} python/numerical_computing -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} python/data_visualization -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} python/build_in_functions -.-> lab-48815{{"`Plotting Lorenz Attractor in 3D using Python`"}} end

Import Libraries

We start by importing the necessary libraries: Matplotlib and NumPy.

import matplotlib.pyplot as plt
import numpy as np

Define the Lorenz Function

We define the Lorenz function, which takes in three parameters and returns an array of three values. We use the default values s=10, r=28, and b=2.667 for the Lorenz parameters.

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])

Set Up the Initial Parameters

We set up the initial parameters for the simulation, including the time step dt, the number of steps num_steps, and the initial values for x, y, and z.

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

Calculate the Lorenz Attractor

We calculate the Lorenz Attractor by stepping through time and estimating the next point using the previous point and the Lorenz function.

for i in range(num_steps):
    xyzs[i + 1] = xyzs[i] + lorenz(xyzs[i]) * dt

Plot the Lorenz Attractor

We plot the Lorenz Attractor using Matplotlib's mplot3d module.

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()

Summary

In this tutorial, we learned how to plot the Lorenz Attractor using Python and Matplotlib. We defined the Lorenz function, set up the initial parameters, calculated the Lorenz Attractor, and plotted it using Matplotlib's mplot3d module.

Other Python Tutorials you may like