{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create a GIF Movie {#gif_movie_example}\n\nGenerate a moving gif from an active plotter.\n\n::: note\n::: title\nNote\n:::\n\nUse `lighting=False` to reduce the size of the color space to avoid\n\\\"jittery\\\" GIFs, especially for the scalar bar.\n:::\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nimport pyvista as pv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create a structured grid\n\nCreate a structured grid and make a \\\"wave\\\" my shifting the Z position\nbased on the cartesian distance from the origin.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.arange(-10, 10, 0.5)\ny = np.arange(-10, 10, 0.5)\nx, y = np.meshgrid(x, y)\nr = np.sqrt(x**2 + y**2)\nz = np.sin(r)\n\n# Create and structured surface\ngrid = pv.StructuredGrid(x, y, z)\ngrid[\"Height\"] = z.ravel()\ngrid.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Generate a GIF\n\nGenerate a GIF using `off_screen=True` parameter.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Create a plotter object and set the scalars to the Z height\nplotter = pv.Plotter(notebook=False, off_screen=True)\nplotter.add_mesh(\n grid,\n scalars=\"Height\",\n lighting=False,\n show_edges=True,\n clim=[-1, 1],\n)\n\n# Open a gif\nplotter.open_gif(\"wave.gif\")\n\n# Update Z and write a frame for each updated position\nnframe = 15\nfor phase in np.linspace(0, 2 * np.pi, nframe + 1)[:nframe]:\n z = np.sin(r + phase)\n # Update values inplace\n grid.points[:, -1] = z.ravel()\n grid[\"Height\"] = z.ravel()\n # Write a frame. This triggers a render.\n plotter.write_frame()\n\n# Closes and finalizes movie\nplotter.close()" ] } ], "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.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }