{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# GridSpec with variable sizes and spacing\n\nThis example demonstrates the use of `.GridSpec` to generate subplots,\nthe control of the relative sizes of subplots with *width_ratios* and\n*height_ratios*, and the control of the spacing around and between subplots\nusing subplot params (*left*, *right*, *bottom*, *top*, *wspace*, and\n*hspace*).\n\n.. redirect-from:: /gallery/userdemo/demo_gridspec03\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nfrom matplotlib.gridspec import GridSpec\n\n\ndef annotate_axes(fig):\n for i, ax in enumerate(fig.axes):\n ax.text(0.5, 0.5, \"ax%d\" % (i+1), va=\"center\", ha=\"center\")\n ax.tick_params(labelbottom=False, labelleft=False)\n\n\nfig = plt.figure()\nfig.suptitle(\"Controlling subplot sizes with width_ratios and height_ratios\")\n\ngs = GridSpec(2, 2, width_ratios=[1, 2], height_ratios=[4, 1])\nax1 = fig.add_subplot(gs[0])\nax2 = fig.add_subplot(gs[1])\nax3 = fig.add_subplot(gs[2])\nax4 = fig.add_subplot(gs[3])\n\nannotate_axes(fig)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig = plt.figure()\nfig.suptitle(\"Controlling spacing around and between subplots\")\n\ngs1 = GridSpec(3, 3, left=0.05, right=0.48, wspace=0.05)\nax1 = fig.add_subplot(gs1[:-1, :])\nax2 = fig.add_subplot(gs1[-1, :-1])\nax3 = fig.add_subplot(gs1[-1, -1])\n\ngs2 = GridSpec(3, 3, left=0.55, right=0.98, hspace=0.05)\nax4 = fig.add_subplot(gs2[:, :-1])\nax5 = fig.add_subplot(gs2[:-1, -1])\nax6 = fig.add_subplot(gs2[-1, -1])\n\nannotate_axes(fig)\n\nplt.show()" ] } ], "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 }