{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Animate a 3D wireframe plot\n\nA very simple \"animation\" of a 3D plot.  See also :doc:`rotate_axes3d_sgskip`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import time\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib import animation\n\nFRAMES = 25\nFPS = 25\n\nfig = plt.figure()\nax = fig.add_subplot(projection='3d')\n\n# Make the X, Y meshgrid.\nxs = np.linspace(-1, 1, 50)\nys = np.linspace(-1, 1, 50)\nX, Y = np.meshgrid(xs, ys)\n\n# Set the z axis limits, so they aren't recalculated each frame.\nax.set_zlim(-1, 1)\n\n# Begin plotting.\nwframe = None\ntstart = time.time()\n\n\ndef animate(i):\n    global wframe\n    # If a line collection is already there, remove it before drawing.\n    if wframe:\n        wframe.remove()\n    # Generate data.\n    phi = i / FRAMES * 2 * np.pi\n    Z = np.cos(2 * np.pi * X + phi) * (1 - np.hypot(X, Y))\n    # Plot the new wireframe.\n    wframe = ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2)\n    if i == FRAMES - 1:  # Print FPS at the end of the loop.\n        global tstart\n        fps = FRAMES / (time.time() - tstart)\n        print(f'Expected FPS: {FPS}; Average FPS: {fps}')\n        tstart = time.time()\n\n\nani = animation.FuncAnimation(fig, animate, interval=1000 / FPS, frames=FRAMES)\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. tags::\n   plot-type: 3D,\n   component: animation,\n   level: beginner\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "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.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}