YouTip LogoYouTip

Matplotlib Ref Layout Config

* * *\n\n[![Image 1: Matplotlib Reference Documentation](https://example.com/images/up.gif) Matplotlib Reference Documentation](https://example.com/matplotlib/matplotlib-apiref.html)\n\nTitle: Matplotlib Layout and Configuration Functions | Online Tutorial\n\nURL Source: https://example.com/matplotlib/matplotlib-ref-layout-config.html\n\nLayout functions control subplot spacing and arrangement, configuration functions manage Matplotlib global parameters.\n\n## Function Overview\n\n### Layout Functions\n\n| Function | Description |\n| --- | --- |\n| subplots_adjust() | Manually adjust subplot spacing |\n| tight_layout() | Automatic compact layout |\n| margins() | Set data margins (whitespace at axis ends) |\n| subplot_tool() | Launch interactive subplot adjustment tool |\n\n### Configuration Functions\n\n| Function | Description |\n| --- | --- |\n| rc() | Set rc parameters (supports batch setting) |\n| rc_context() | Context manager for temporarily setting rc parameters |\n| rcdefaults() | Restore all rc parameters to default values |\n\n### Colormap Functions\n\n| Function | Description |\n| --- | --- |\n| clim() | Set the data range for colormap |\n| get_cmap() | Get colormap by specified name |\n| set_cmap() | Set default colormap |\n| gci() | Get current mappable object |\n| sci() | Set current mappable object |\n| imread() | Read image from file into array |\n| imsave() | Save array as image file |\n\n### Output and Interaction Functions\n\n| Function | Description |\n| --- | --- |\n| show() | Display all open Figures |\n| draw() | Force redraw current Figure |\n| draw_if_interactive() | Render Figure in interactive mode |\n| pause() | Pause for specified seconds (process GUI events during) |\n| ion() / ioff() | Turn interactive mode on/off |\n| isinteractive() | Check if in interactive mode |\n| install_repl_displayhook() | Install REPL display hook |\n| uninstall_repl_displayhook() | Uninstall REPL display hook |\n| switch_backend() | Switch Matplotlib backend |\n\n* * *\n\n## Layout Function Examples\n\n### subplots_adjust() / tight_layout() / margins()\n\nmatplotlib.pyplot.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None) matplotlib.pyplot.tight_layout(*, pad=1.08, h_pad=None, w_pad=None, rect=None) matplotlib.pyplot.margins(*margins, x=None, y=None, tight=True)\n\n## Example\n\nimport matplotlib.pyplot as plt\n\nimport numpy as np\n\nx = np.linspace(0,10,100)\n\nfig, axes = plt.subplots(2,2, figsize=(10,8))\n\n# subplots_adjust manual control\n\n fig.subplots_adjust(left=0.1, right=0.95,\n\ntop=0.9, bottom=0.1,\n\nhspace=0.4, wspace=0.3)\n\nfor i, ax in enumerate(axes.flatten()):\n\n ax.plot(x, np.sin(x + i))\n\n ax.set_title(f'Plot {i+1}')\n\n ax.set_xlabel('A long x label')\n\n ax.set_ylabel('A long y label')\n\n# set margins\n\n ax.margins(x=0.1, y=0.15)\n\nfig.suptitle('subplots_adjust() + margins()', fontsize=14)\n\n plt.show()\n\nprint("tutorial: layout example")\n\n* * *\n\n## Configuration Function Examples\n\n### rc() / rc_context() / rcdefaults()\n\nmatplotlib.pyplot.rc(group, **kwargs) matplotlib.pyplot.rc_context(rc=None, fname=None) matplotlib.pyplot.rcdefaults()\n\n## Example\n\nimport matplotlib.pyplot as plt\n\nimport numpy as np\n\n# Method 1: Global setting rcParams\n\n plt.rcParams['font.size']=12\n\n plt.rcParams['axes.grid']=True\n\n# Method 2: Batch setting with rc()\n\n plt.rc('lines', linewidth=2, color='steelblue')\n\n plt.rc('axes', titlesize=14, grid=True)\n\nx = np.linspace(0,10,50)\n\n# rc_context temporary setting (only effective within with block)\n\n fig,(ax1, ax2)= plt.subplots(1,2, figsize=(10,4),\n\n layout='constrained')\n\n# ax1 uses global rc settings\n\n ax1.plot(x, np.sin(x))\n\n ax1.set_title('Global rc settings')\n\n# ax2 uses temporary style override\n\nwith plt.rc_context({'lines.color': 'coral',\n\n'lines.linestyle': '--',\n\n'axes.facecolor': '#f0f0f0'}):\n\n ax2.plot(x, np.cos(x))\n\n ax2.set_title('rc_context() overrides')\n\nplt.show()\n\n# restore defaults\n\n plt.rcdefaults()\n\nprint("tutorial: rc settings restored to defaults")\n\n* * *\n\n## Colormap Function Examples\n\n### clim() / get_cmap() / set_cmap()\n\nmatplotlib.pyplot.clim(vmin=None, vmax=None) matplotlib.pyplot.get_cmap(name=None, lut=None) matplotlib.pyplot.set_cmap(cmap)\n\n## Example\n\nimport matplotlib.pyplot as plt\n\nimport numpy as np\n\ndata = np.random.rand(
← Matplotlib Ref PieMatplotlib Ref Imshow β†’