{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Manage multiple figures in pyplot\n\n`matplotlib.pyplot` uses the concept of a *current figure* and *current Axes*.\nFigures are identified via a figure number that is passed to `~.pyplot.figure`.\nThe figure with the given number is set as *current figure*. Additionally, if\nno figure with the number exists, a new one is created.\n\n

Note

We discourage working with multiple figures through the implicit pyplot\n interface because managing the *current figure* is cumbersome and\n error-prone. Instead, we recommend using the explicit approach and call\n methods on Figure and Axes instances. See `api_interfaces` for an\n explanation of the trade-offs between the implicit and explicit interfaces.

\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nt = np.arange(0.0, 2.0, 0.01)\ns1 = np.sin(2*np.pi*t)\ns2 = np.sin(4*np.pi*t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create figure 1\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.figure(1)\nplt.subplot(211)\nplt.plot(t, s1)\nplt.subplot(212)\nplt.plot(t, 2*s1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create figure 2\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.figure(2)\nplt.plot(t, s2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now switch back to figure 1 and make some changes\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.figure(1)\nplt.subplot(211)\nplt.plot(t, s2, 's')\nax = plt.gca()\nax.set_xticklabels([])\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags:: component: figure, plot-type: line\n\n" ] } ], "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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }