{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n.. redirect-from:: /tutorial/intermediate/tight_layout_guide\n\n\n# Tight layout guide\n\nHow to use tight-layout to fit plots within your figure cleanly.\n\n*tight_layout* automatically adjusts subplot params so that the\nsubplot(s) fits in to the figure area. This is an experimental\nfeature and may not work for some cases. It only checks the extents\nof ticklabels, axis labels, and titles.\n\nAn alternative to *tight_layout* is `constrained_layout\n`.\n\n\n## Simple example\n\nWith the default Axes positioning, the axes title, axis labels, or tick labels\ncan sometimes go outside the figure area, and thus get clipped.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nplt.rcParams['savefig.facecolor'] = \"0.8\"\n\n\ndef example_plot(ax, fontsize=12):\n ax.plot([1, 2])\n\n ax.locator_params(nbins=3)\n ax.set_xlabel('x-label', fontsize=fontsize)\n ax.set_ylabel('y-label', fontsize=fontsize)\n ax.set_title('Title', fontsize=fontsize)\n\nplt.close('all')\nfig, ax = plt.subplots()\nexample_plot(ax, fontsize=24)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To prevent this, the location of Axes needs to be adjusted. For\nsubplots, this can be done manually by adjusting the subplot parameters\nusing `.Figure.subplots_adjust`. `.Figure.tight_layout` does this\nautomatically.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\nexample_plot(ax, fontsize=24)\nplt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that :func:`matplotlib.pyplot.tight_layout` will only adjust the\nsubplot params when it is called. In order to perform this adjustment each\ntime the figure is redrawn, you can call ``fig.set_tight_layout(True)``, or,\nequivalently, set :rc:`figure.autolayout` to ``True``.\n\nWhen you have multiple subplots, often you see labels of different\nAxes overlapping each other.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.close('all')\n\nfig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\nexample_plot(ax4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ":func:`~matplotlib.pyplot.tight_layout` will also adjust spacing between\nsubplots to minimize the overlaps.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\nexample_plot(ax4)\nplt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ":func:`~matplotlib.pyplot.tight_layout` can take keyword arguments of\n*pad*, *w_pad* and *h_pad*. These control the extra padding around the\nfigure border and between subplots. The pads are specified in fraction\nof fontsize.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\nexample_plot(ax4)\nplt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ":func:`~matplotlib.pyplot.tight_layout` will work even if the sizes of\nsubplots are different as far as their grid specification is\ncompatible. In the example below, *ax1* and *ax2* are subplots of a 2x2\ngrid, while *ax3* is of a 1x2 grid.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.close('all')\nfig = plt.figure()\n\nax1 = plt.subplot(221)\nax2 = plt.subplot(223)\nax3 = plt.subplot(122)\n\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\n\nplt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It works with subplots created with\n:func:`~matplotlib.pyplot.subplot2grid`. In general, subplots created\nfrom the gridspec (`arranging_axes`) will work.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.close('all')\nfig = plt.figure()\n\nax1 = plt.subplot2grid((3, 3), (0, 0))\nax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)\nax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)\nax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)\n\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\nexample_plot(ax4)\n\nplt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Although not thoroughly tested, it seems to work for subplots with\naspect != \"auto\" (e.g., Axes with images).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "arr = np.arange(100).reshape((10, 10))\n\nplt.close('all')\nfig = plt.figure(figsize=(5, 4))\n\nax = plt.subplot()\nim = ax.imshow(arr, interpolation=\"none\")\n\nplt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Caveats\n\n* `~matplotlib.pyplot.tight_layout` considers all artists on the Axes by\n default. To remove an artist from the layout calculation you can call\n `.Artist.set_in_layout`.\n\n* ``tight_layout`` assumes that the extra space needed for artists is\n independent of the original location of Axes. This is often true, but there\n are rare cases where it is not.\n\n* ``pad=0`` can clip some texts by a few pixels. This may be a bug or\n a limitation of the current algorithm, and it is not clear why it\n happens. Meanwhile, use of pad larger than 0.3 is recommended.\n\n* The algorithm of ``tight_layout`` does not necessarily converge,\n i.e. calling ``tight_layout`` multiple times can lead to slight\n variations in the layout between the calls.\n\n## Use with GridSpec\n\nGridSpec has its own `.GridSpec.tight_layout` method (the pyplot api\n`.pyplot.tight_layout` also works).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.gridspec as gridspec\n\nplt.close('all')\nfig = plt.figure()\n\ngs1 = gridspec.GridSpec(2, 1)\nax1 = fig.add_subplot(gs1[0])\nax2 = fig.add_subplot(gs1[1])\n\nexample_plot(ax1)\nexample_plot(ax2)\n\ngs1.tight_layout(fig)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You may provide an optional *rect* parameter, which specifies the bounding\nbox that the subplots will be fit inside. The coordinates are in\nnormalized figure coordinates and default to (0, 0, 1, 1) (the whole figure).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig = plt.figure()\n\ngs1 = gridspec.GridSpec(2, 1)\nax1 = fig.add_subplot(gs1[0])\nax2 = fig.add_subplot(gs1[1])\n\nexample_plot(ax1)\nexample_plot(ax2)\n\ngs1.tight_layout(fig, rect=[0, 0, 0.5, 1.0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, we do not recommend that this be used to manually construct more\ncomplicated layouts, like having one GridSpec in the left and one in the\nright side of the figure. For these use cases, one should instead take\nadvantage of :doc:`/gallery/subplots_axes_and_figures/gridspec_nested`, or\nthe :doc:`/gallery/subplots_axes_and_figures/subfigures`.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Legends and annotations\n\nPre Matplotlib 2.2, legends and annotations were excluded from the bounding\nbox calculations that decide the layout. Subsequently, these artists were\nadded to the calculation, but sometimes it is undesirable to include them.\nFor instance in this case it might be good to have the Axes shrink a bit\nto make room for the legend:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(4, 3))\nlines = ax.plot(range(10), label='A simple plot')\nax.legend(bbox_to_anchor=(0.7, 0.5), loc='center left',)\nfig.tight_layout()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, sometimes this is not desired (quite often when using\n``fig.savefig('outname.png', bbox_inches='tight')``). In order to\nremove the legend from the bounding box calculation, we simply set its\nbounding ``leg.set_in_layout(False)`` and the legend will be ignored.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(4, 3))\nlines = ax.plot(range(10), label='B simple plot')\nleg = ax.legend(bbox_to_anchor=(0.7, 0.5), loc='center left',)\nleg.set_in_layout(False)\nfig.tight_layout()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use with AxesGrid1\n\nLimited support for :mod:`mpl_toolkits.axes_grid1` is provided.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from mpl_toolkits.axes_grid1 import Grid\n\nplt.close('all')\nfig = plt.figure()\ngrid = Grid(fig, rect=111, nrows_ncols=(2, 2),\n axes_pad=0.25, label_mode='L',\n )\n\nfor ax in grid:\n example_plot(ax)\nax.title.set_visible(False)\n\nplt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Colorbar\n\nIf you create a colorbar with `.Figure.colorbar`, the created colorbar is\ndrawn in a Subplot as long as the parent Axes is also a Subplot, so\n`.Figure.tight_layout` will work.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.close('all')\narr = np.arange(100).reshape((10, 10))\nfig = plt.figure(figsize=(4, 4))\nim = plt.imshow(arr, interpolation=\"none\")\n\nplt.colorbar(im)\n\nplt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another option is to use the AxesGrid1 toolkit to\nexplicitly create an Axes for the colorbar.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from mpl_toolkits.axes_grid1 import make_axes_locatable\n\nplt.close('all')\narr = np.arange(100).reshape((10, 10))\nfig = plt.figure(figsize=(4, 4))\nim = plt.imshow(arr, interpolation=\"none\")\n\ndivider = make_axes_locatable(plt.gca())\ncax = divider.append_axes(\"right\", \"5%\", pad=\"3%\")\nplt.colorbar(im, cax=cax)\n\nplt.tight_layout()" ] } ], "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 }