{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Timers\n\nSimple example of using general timer objects. This is used to update\nthe time placed in the title of the figure.\n\n
Note
\n\nThis example exercises the interactive capabilities of Matplotlib, and this\nwill not appear in the static documentation. Please run this code on your\nmachine to see the interactivity.\n\nYou can copy and paste individual parts, or download the entire example\nusing the link at the bottom of the page.\n\n
\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from datetime import datetime\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n\ndef update_title(axes):\n axes.set_title(datetime.now())\n axes.figure.canvas.draw()\n\nfig, ax = plt.subplots()\n\nx = np.linspace(-3, 3)\nax.plot(x, x ** 2)\n\n# Create a new timer object. Set the interval to 100 milliseconds\n# (1000 is default) and tell the timer what function should be called.\ntimer = fig.canvas.new_timer(interval=100)\ntimer.add_callback(update_title, ax)\ntimer.start()\n\n# Or could start the timer on first figure draw:\n# def start_timer(event):\n# timer.start()\n# fig.canvas.mpl_disconnect(drawid)\n# drawid = fig.canvas.mpl_connect('draw_event', start_timer)\n\nplt.show()"
]
}
],
"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.15"
}
},
"nbformat": 4,
"nbformat_minor": 0
}