Matplotlib Figure and Axes Management Functions
\n\nURL Source: https://example.com/matplotlib/matplotlib-ref-figure-management.html
\n\n\n\n
Matplotlib Reference Documentation
Matplotlib provides a series of functions for creating and managing Figure and Axes objects. All related functions are listed below.
\n\nFunction List
\n\n| Function | \nDescription | \npyplot | \nCorresponding Method | \n
|---|---|---|---|
| figure() | \nCreate or activate Figure | \nplt.figure() | \n- | \n
| subplots() | \nCreate Figure and Axes grid | \nplt.subplots() | \nfig.subplots() | \n
| subplot() | \nAdd single subplot by row/column index | \nplt.subplot() | \nfig.add_subplot() | \n
| subplot2grid() | \nCreate subplot at specified grid position | \nplt.subplot2grid() | \n- | \n
| subplot_mosaic() | \nLayout subplots with label string | \nplt.subplot_mosaic() | \nfig.subplot_mosaic() | \n
| axes() | \nAdd Axes to current Figure | \nplt.axes() | \nfig.add_axes() | \n
| gca() | \nGet current Axes | \nplt.gca() | \nfig.gca() | \n
| gcf() | \nGet current Figure | \nplt.gcf() | \n- | \n
| sca() | \nSet current Axes | \nplt.sca(ax) | \nfig.sca(ax) | \n
| cla() | \nClear current Axes | \nplt.cla() | \nax.cla() | \n
| clf() | \nClear current Figure | \nplt.clf() | \nfig.clear() | \n
| close() | \nClose Figure window | \nplt.close() | \n- | \n
| delaxes() | \nRemove Axes from Figure | \nplt.delaxes(ax) | \nfig.delaxes(ax) | \n
| fignum_exists() | \nCheck if Figure number exists | \nplt.fignum_exists(n) | \n- | \n
| get_figlabels() | \nGet all Figure labels list | \nplt.get_figlabels() | \n- | \n
| get_fignums() | \nGet all Figure numbers list | \nplt.get_fignums() | \n- | \n
| twinx() | \nCreate dual y-axes sharing x-axis | \nplt.twinx() | \nax.twinx() | \n
| twiny() | \nCreate dual x-axes sharing y-axis | \nplt.twiny() | \nax.twiny() | \n
Usage Examples
\n\nExample 1: gca/gcf/sca - Operate on Current Chart
\n\nInstance
\n\nimport matplotlib.pyplot as plt\n\nimport numpy as np\n\nx = np.linspace(0,10,100)\n\n# Create first Figure\n\n plt.figure(1)\n\n plt.plot(x, np.sin(x), label='sin(x)')\n\n# Create second Figure\n\n plt.figure(2)\n\n plt.plot(x, np.cos(x), label='cos(x)')\n\n# Check if Figure 1 exists\n\nprint(plt.fignum_exists(1))# True\n\n# Get all Figure numbers\n\nprint(plt.get_fignums())# [1, 2]\n\n# Switch back to Figure 1\n\n plt.figure(1)\n\n ax = plt.gca()# Get current Axes\n\nprint(ax.get_title())\n\n# Set current Axes to another\n\n plt.sca(ax)\n\n plt.title('Switched via sca()')\n\nplt.show()\n\n\nTrue\n[1, 2]\n\n\nExample 2: subplot2grid - Create Non-Uniform Layout
\n\nInstance
\n\nimport matplotlib.pyplot as plt\n\nimport numpy as np\n\nfig = plt.figure(figsize=(10,6), layout='constrained')\n\n# subplot2grid(shape, loc, rowspan, colspan)\n\n# shape=(3,3): 3line3columngrid\n\n ax1 = plt.subplot2grid((3,3),(0,0), colspan=3)# spans the entire top row\n\n ax2 = plt.subplot2grid((3,3),(1,0), rowspan=2)# spans 2 rows on the left\n\n ax3 = plt.subplot2grid((3,3),(1,1), colspan=2)# spans 2 columns at the top right\n\n ax4 = plt.subplot2grid((3,3),(2,1))# Middle Right\n\n ax5 = plt.subplot2grid((3,3),(2,2))# Bottom Right\n\nx = np.linspace(0,10,50)\n\n ax1.plot(x, np.sin(x))\n\n ax1.set_title('Top: colspan=3')\n\nax2.plot(x, np.cos(x),'orange')\n\n ax2.set_title('Left: rowspan=2')\n\nax3.bar(['A','B','C'],[3,7,5])\n\n ax3.set_title('Right Top: colspan=2')\n\nax4.text(0.5,0.5,'Panel 4', ha='center', va='center')\n\n ax5.text(0.5,0.5,'Panel 5', ha='center', va='center')\n\nfig.suptitle('subplot2grid Layout', fontsize=14)\n\n plt.show()\n\n\n
YouTip