Last active
October 22, 2023 23:25
-
-
Save branning/c8e63ce81be0391260b1 to your computer and use it in GitHub Desktop.
Matplotlib nbagg example that updates in a loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"kernelspec": { | |
"name": "python", | |
"display_name": "Python (Pyodide)", | |
"language": "python" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "python", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8" | |
} | |
}, | |
"nbformat_minor": 4, | |
"nbformat": 4, | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"source": "import micropip\nawait micropip.install(\"matplotlib\")\nawait micropip.install(\"ipympl\")", | |
"metadata": { | |
"trusted": true | |
}, | |
"outputs": [], | |
"execution_count": null | |
}, | |
{ | |
"cell_type": "code", | |
"source": "import numpy as np\nimport matplotlib.pyplot as plt\nimport time", | |
"metadata": { | |
"trusted": true | |
}, | |
"outputs": [], | |
"execution_count": null | |
}, | |
{ | |
"cell_type": "code", | |
"source": "%matplotlib widget\ndef pltsin(ax, colors=['b']):\n x = np.linspace(0,1,100)\n if ax.lines:\n for line in ax.lines:\n line.set_xdata(x)\n y = np.random.random(size=(100,1))\n line.set_ydata(y)\n else:\n for color in colors:\n y = np.random.random(size=(100,1))\n ax.plot(x, y, color)\n fig.canvas.draw()", | |
"metadata": { | |
"trusted": true | |
}, | |
"outputs": [], | |
"execution_count": null | |
}, | |
{ | |
"cell_type": "code", | |
"source": "fig,ax = plt.subplots(1,1)\nax.set_xlabel('X')\nax.set_ylabel('Y')\nax.set_xlim(0,1)\nax.set_ylim(0,1)\nplt.show()", | |
"metadata": { | |
"trusted": true | |
}, | |
"outputs": [], | |
"execution_count": null | |
}, | |
{ | |
"cell_type": "code", | |
"source": "for f in range(5):\n pltsin(ax, ['b', 'r'])\n time.sleep(1)", | |
"metadata": { | |
"trusted": true | |
}, | |
"outputs": [], | |
"execution_count": null | |
}, | |
{ | |
"cell_type": "code", | |
"source": "", | |
"metadata": { | |
"trusted": true | |
}, | |
"outputs": [], | |
"execution_count": null | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment