{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Advanced \n", "\n", "## Matplotlib an manim combined" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from manim import *\n", "import matplotlib.pyplot as plt\n", "\n", "param = \"-v WARNING -s -ql --disable_caching --progress_bar None Example\"\n", "paramH = \"-v WARNING -s -qh --disable_caching --progress_bar None Example\"\n", "paramp = \"-v WARNING -ql --disable_caching --progress_bar None Example\"\n", "parampH = \"-v WARNING -qh --disable_caching --progress_bar None Example\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%manim $paramp\n", "\n", "plt.rcParams['figure.dpi'] = 100\n", "\n", "def my_function(amplitude, x):\n", " return amplitude * np.sin(x)\n", "\n", "def mpl_image_plt(amplitude, x):\n", " fig, ax = plt.subplots()\n", " ax.plot(x, my_function(amplitude, x))\n", " ax.set_ylim(-1, 1)\n", " fig.canvas.draw()\n", " img = ImageMobject(fig.canvas.buffer_rgba()).scale(2)\n", " plt.close(fig)\n", " return img\n", "\n", "class Example(Scene):\n", " def construct(self):\n", " self.camera.background_color=WHITE\n", " x_values = np.linspace(0, 30, 400)\n", " amp1 = 0.5\n", " amp2 = 1\n", " tr_amplitude = ValueTracker(amp1)\n", " image = mpl_image_plt(amp1, x_values)\n", " self.add(image)\n", "\n", " def update_image(mob):\n", " new_mob = mpl_image_plt(tr_amplitude.get_value(), x_values)\n", " mob.become(new_mob)\n", "\n", " image.add_updater(update_image)\n", " self.play(tr_amplitude.animate.set_value(amp2), run_time=3)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%manim $paramp\n", "\n", "plt.rcParams['figure.dpi'] = 300\n", "\n", "class Example(Scene):\n", " def construct(self):\n", " self.camera.background_color= WHITE\n", " fig = plt.figure()\n", " ax = fig.add_subplot(projection='3d')\n", " x=np.full((21,27,27),0)\n", " x[10,10,10]=1\n", " x[10,16,10]=1\n", " ax.voxels(x, edgecolor='k')\n", " fig.canvas.draw()\n", " buf = fig.canvas.buffer_rgba()\n", " img = ImageMobject(buf).scale(1)\n", " plt.close(fig)\n", " self.add(img)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%manim $paramp\n", "\n", "plt.rcParams['figure.dpi'] = 300\n", "\n", "def my_function(amplitude, x):\n", " return amplitude * np.sin(x)\n", "\n", "def mpl_image3d_plt(amp1,x):\n", " fig = plt.figure()\n", " ax = fig.add_subplot(projection='3d')\n", " ax.view_init(elev=40+3*np.sin(amp1), azim=20+10*np.cos(amp1))\n", " ax.voxels(x, edgecolor='k')\n", " ax.axes.xaxis.set_ticklabels([])\n", " ax.axes.yaxis.set_ticklabels([])\n", " ax.axes.zaxis.set_ticklabels([])\n", " fig.canvas.draw()\n", " buf = fig.canvas.buffer_rgba()\n", " img = ImageMobject(buf).scale(1)\n", " plt.close(fig)\n", " return img\n", "\n", "\n", "class Example(Scene):\n", " def construct(self):\n", " self.camera.background_color= WHITE\n", " x=np.full((21,27,27),0)\n", " x[10,10,10]=1\n", " x[10,16,10]=1\n", " amp1=0\n", " amp2=TAU\n", " tr_amplitude = ValueTracker(amp1)\n", " image = mpl_image3d_plt(amp1, x)\n", " self.add(image)\n", "\n", " def update_image(mob):\n", " new_mob = mpl_image3d_plt(tr_amplitude.get_value(), x)\n", " mob.become(new_mob)\n", "\n", " image.add_updater(update_image)\n", " self.play(tr_amplitude.animate.set_value(amp2), run_time=4)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Animations with OpenGL\n", "\n", "Just add the `--renderer=opengl` flag, and animations will render with the OpenGL backend, which is still under development.\n", "Here are two examples: one for rendering a video (needs additional the `--write_to_movie` flag) and one for a static 3D scene (including it)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "paramGL = \"-v WARNING -s -ql --renderer=opengl --disable_caching --progress_bar None Example\"\n", "paramHGL = \"-v WARNING -s -qh --renderer=opengl --disable_caching --progress_bar None Example\"\n", "parampGL = \"-v WARNING -ql --renderer=opengl --write_to_movie --disable_caching --progress_bar None Example\"\n", "parampHGL = \"-v WARNING -qh --renderer=opengl --write_to_movie --disable_caching --progress_bar None Example\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time\n", "%%manim $parampHGL\n", "class Example(Scene):\n", " def construct(self):\n", " dot= Dot(color= YELLOW, radius=0.5)\n", " self.play(dot.animate.shift(2*RIGHT).scale(2).set_color(BLUE))\n", " self.wait()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time\n", "%%manim $parampGL\n", "class Example(ThreeDScene):\n", " def construct(self):\n", " resolution_fa = 42\n", " self.set_camera_orientation(phi=75 * DEGREES, theta=-30 * DEGREES)\n", "\n", " def param_gauss(u, v):\n", " x = u\n", " y = v\n", " sigma, mu = 0.4, [0.0, 0.0]\n", " d = np.linalg.norm(np.array([x - mu[0], y - mu[1]]))\n", " z = np.exp(-(d ** 2 / (2.0 * sigma ** 2)))\n", " return np.array([x, y, z])\n", "\n", " gauss_plane = Surface(\n", " param_gauss,\n", " resolution=(resolution_fa, resolution_fa),\n", " v_range=[-2, +2],\n", " u_range=[-2, +2]\n", " )\n", "\n", " gauss_plane.scale(2, about_point=ORIGIN)\n", " gauss_plane.set_style(fill_opacity=1,stroke_color=GREEN)\n", " gauss_plane.set_fill_by_checkerboard(ORANGE, BLUE, opacity=0.5)\n", " axes = ThreeDAxes()\n", " self.add(axes,gauss_plane)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time \n", "%%manim $param\n", "## comparison of the same example to cairo:\n", "class Example(ThreeDScene):\n", " def construct(self):\n", " resolution_fa = 42\n", " self.set_camera_orientation(phi=75 * DEGREES, theta=-30 * DEGREES)\n", "\n", " def param_gauss(u, v):\n", " x = u\n", " y = v\n", " sigma, mu = 0.4, [0.0, 0.0]\n", " d = np.linalg.norm(np.array([x - mu[0], y - mu[1]]))\n", " z = np.exp(-(d ** 2 / (2.0 * sigma ** 2)))\n", " return np.array([x, y, z])\n", "\n", " gauss_plane = Surface(\n", " param_gauss,\n", " resolution=(resolution_fa, resolution_fa),\n", " v_range=[-2, +2],\n", " u_range=[-2, +2]\n", " )\n", "\n", " gauss_plane.scale(2, about_point=ORIGIN)\n", " gauss_plane.set_style(fill_opacity=1,stroke_color=GREEN)\n", " gauss_plane.set_fill_by_checkerboard(ORANGE, BLUE, opacity=0.5)\n", " axes = ThreeDAxes()\n", " self.add(axes,gauss_plane)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "interpreter": { "hash": "deefdb209dc179ecbc880626fece8554a87baca41cae0d7b198759a4c27f783b" }, "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.9.5" } }, "nbformat": 4, "nbformat_minor": 4 }