{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Colorbar with AxesDivider\n\nThe `.axes_divider.make_axes_locatable` function takes an existing Axes, adds\nit to a new `.AxesDivider` and returns the `.AxesDivider`. The `.append_axes`\nmethod of the `.AxesDivider` can then be used to create a new Axes on a given\nside (\"top\", \"right\", \"bottom\", or \"left\") of the original Axes. This example\nuses `.append_axes` to add colorbars next to Axes.\n\nUsers should consider simply passing the main Axes to the *ax* keyword argument of\n`~.Figure.colorbar` instead of creating a locatable Axes manually like this.\nSee `colorbar_placement`.\n\n.. redirect-from:: /gallery/axes_grid1/simple_colorbar\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nfrom mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable\n\nfig, (ax1, ax2) = plt.subplots(1, 2)\nfig.subplots_adjust(wspace=0.5)\n\nim1 = ax1.imshow([[1, 2], [3, 4]])\nax1_divider = make_axes_locatable(ax1)\n# Add an Axes to the right of the main Axes.\ncax1 = ax1_divider.append_axes(\"right\", size=\"7%\", pad=\"2%\")\ncb1 = fig.colorbar(im1, cax=cax1)\n\nim2 = ax2.imshow([[1, 2], [3, 4]])\nax2_divider = make_axes_locatable(ax2)\n# Add an Axes above the main Axes.\ncax2 = ax2_divider.append_axes(\"top\", size=\"7%\", pad=\"2%\")\ncb2 = fig.colorbar(im2, cax=cax2, orientation=\"horizontal\")\n# Change tick position to top (with the default tick position \"bottom\", ticks\n# overlap the image).\ncax2.xaxis.set_ticks_position(\"top\")\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.5" } }, "nbformat": 4, "nbformat_minor": 0 }