YouTip LogoYouTip

Matplotlib Ref Figure Management

Matplotlib Figure and Axes Management Functions

\n\n

URL Source: https://example.com/matplotlib/matplotlib-ref-figure-management.html

\n\n
\n\n

Image 1: Matplotlib Reference Documentation Matplotlib Reference Documentation

\n\n

Matplotlib provides a series of functions for creating and managing Figure and Axes objects. All related functions are listed below.

\n\n

Function List

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
FunctionDescriptionpyplotCorresponding Method
figure()Create or activate Figureplt.figure()-
subplots()Create Figure and Axes gridplt.subplots()fig.subplots()
subplot()Add single subplot by row/column indexplt.subplot()fig.add_subplot()
subplot2grid()Create subplot at specified grid positionplt.subplot2grid()-
subplot_mosaic()Layout subplots with label stringplt.subplot_mosaic()fig.subplot_mosaic()
axes()Add Axes to current Figureplt.axes()fig.add_axes()
gca()Get current Axesplt.gca()fig.gca()
gcf()Get current Figureplt.gcf()-
sca()Set current Axesplt.sca(ax)fig.sca(ax)
cla()Clear current Axesplt.cla()ax.cla()
clf()Clear current Figureplt.clf()fig.clear()
close()Close Figure windowplt.close()-
delaxes()Remove Axes from Figureplt.delaxes(ax)fig.delaxes(ax)
fignum_exists()Check if Figure number existsplt.fignum_exists(n)-
get_figlabels()Get all Figure labels listplt.get_figlabels()-
get_fignums()Get all Figure numbers listplt.get_fignums()-
twinx()Create dual y-axes sharing x-axisplt.twinx()ax.twinx()
twiny()Create dual x-axes sharing y-axisplt.twiny()ax.twiny()
\n\n

Usage Examples

\n\n

Example 1: gca/gcf/sca - Operate on Current Chart

\n\n

Instance

\n\n
import 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\n
True\n[1, 2]\n
\n\n

Example 2: subplot2grid - Create Non-Uniform Layout

\n\n

Instance

\n\n
import 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

Example 3: subplot_mosaic - Label Layout

← Matplotlib Ref Fill_BetweenMatplotlib Ref Contour β†’