{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Tutorial 5 - 3D plotting\n", "\n", "This tutorial draws a Lorenz System, it reproduces a tutorial from [Tikz-Python](https://github.com/ltrujello/Tikz-Python/blob/main/examples/lorenz/lorenz.py)." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "from tikzpics import TikzFigure" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "# source: https://github.com/ltrujello/Tikz-Python/blob/main/examples/lorenz/lorenz.py\n", "import numpy as np\n", "from scipy.integrate import odeint\n", "\n", "# lorenz parameters\n", "rho = 28.0\n", "sigma = 10.0\n", "beta = 8.0 / 3.0\n", "\n", "\n", "# Next state according to the ODEs\n", "def next(*state):\n", " x, y, z = state[0][0], state[0][1], state[0][2]\n", " return sigma * (y - x), x * (rho - z) - y, x * y - beta * z\n", "\n", "\n", "# Set initial conditions and time steps\n", "initial = [1.0, 1.0, 1.0]\n", "t = np.arange(0.0, 20.0, 0.01)\n", "\n", "# Solve for the next positions, scale them\n", "states = odeint(func=next, y0=initial, t=t)\n", "states = states * 0.25\n", "\n", "xvec = states[:, 0]\n", "yvec = states[:, 1]\n", "zvec = states[:, 2]\n", "\n", "# Plot the lorenz system\n", "fig = TikzFigure(ndim=3)\n", "options = [\"color=black\", \"thick\"]\n", "\n", "fig.plot3d(xvec, yvec, zvec, options=options, layer=0)\n", "fig.add_node(\n", " x=xvec[0],\n", " y=yvec[0],\n", " z=zvec[0],\n", " fill=\"green!50!black\",\n", " inner_sep=\"2.0pt\",\n", " options=\"circle\",\n", ")\n", "fig.add_node(\n", " x=xvec[-1],\n", " y=yvec[-1],\n", " z=zvec[-1],\n", " fill=\"red!50!black\",\n", " inner_sep=\"2.0pt\",\n", " options=\"circle\",\n", ")\n", "\n", "fig.show(width=500)" ] } ], "metadata": { "kernelspec": { "display_name": "env_tikzpics", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.3" } }, "nbformat": 4, "nbformat_minor": 5 }