{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Combine two subplots using subplots and GridSpec\n\nSometimes we want to combine two subplots in an Axes layout created with\n`~.Figure.subplots`. We can get the `~.gridspec.GridSpec` from the Axes\nand then remove the covered Axes and fill the gap with a new bigger Axes.\nHere we create a layout with the bottom two Axes in the last column combined.\n\nTo start with this layout (rather than removing the overlapping Axes) use\n`~.pyplot.subplot_mosaic`.\n\nSee also `arranging_axes`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nfig, axs = plt.subplots(ncols=3, nrows=3)\ngs = axs[1, 2].get_gridspec()\n# remove the underlying Axes\nfor ax in axs[1:, -1]:\n ax.remove()\naxbig = fig.add_subplot(gs[1:, -1])\naxbig.annotate('Big Axes \\nGridSpec[1:, -1]', (0.1, 0.5),\n xycoords='axes fraction', va='center')\n\nfig.tight_layout()\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags::\n\n component: subplot\n level: beginner\n\n" ] } ], "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 }