Skip to content

Instantly share code, notes, and snippets.

@alexlib
Created February 10, 2025 15:24
Show Gist options
  • Save alexlib/744bd8b606e6276db1e08c2d206f7417 to your computer and use it in GitHub Desktop.
Save alexlib/744bd8b606e6276db1e08c2d206f7417 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.animation as animation\n",
"\n",
"# Define the range for x and a\n",
"x = np.arange(-2, 2.005, 0.005)\n",
"a = np.arange(0, 1705, 5)\n",
"\n",
"# Initialize y with zeros\n",
"y = np.zeros_like(x)\n",
"\n",
"# Create a figure and a line object\n",
"fig, ax = plt.subplots()\n",
"line, = ax.plot(x, y, 'r')\n",
"\n",
"# Set the axis limits\n",
"ax.set_xlim(-2, 2)\n",
"ax.set_ylim(-2, 3)\n",
"\n",
"# Update function for the animation\n",
"def update(i):\n",
" y = np.cbrt(x**2) + 1.1 * np.sin(i * x) * np.sqrt(np.maximum(0, 4 - x**2))\n",
" line.set_ydata(y)\n",
" return line,\n",
"\n",
"# Create the animation\n",
"ani = animation.FuncAnimation(fig, update, frames=a, interval=100, blit=True)\n",
"\n",
"# Save the animation as a GIF\n",
"ani.save('heart.gif', writer='pillow')\n",
"# Display the animation\n",
"plt.show()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "napari",
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@alexlib
Copy link
Author

alexlib commented Feb 10, 2025

heart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment