{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Axes divider\n\nAxes divider to calculate location of Axes and\ncreate a divider for them using existing Axes instances.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nfrom matplotlib import cbook\n\n\ndef get_demo_image():\n z = cbook.get_sample_data(\"axes_grid/bivariate_normal.npy\") # 15x15 array\n return z, (-3, 4, -4, 3)\n\n\ndef demo_simple_image(ax):\n Z, extent = get_demo_image()\n\n im = ax.imshow(Z, extent=extent)\n cb = plt.colorbar(im)\n cb.ax.yaxis.set_tick_params(labelright=False)\n\n\ndef demo_locatable_axes_hard(fig):\n from mpl_toolkits.axes_grid1 import Size, SubplotDivider\n\n divider = SubplotDivider(fig, 2, 2, 2, aspect=True)\n\n # Axes for image\n ax = fig.add_subplot(axes_locator=divider.new_locator(nx=0, ny=0))\n # Axes for colorbar\n ax_cb = fig.add_subplot(axes_locator=divider.new_locator(nx=2, ny=0))\n\n divider.set_horizontal([\n Size.AxesX(ax), # main Axes\n Size.Fixed(0.05), # padding, 0.1 inch\n Size.Fixed(0.2), # colorbar, 0.3 inch\n ])\n divider.set_vertical([Size.AxesY(ax)])\n\n Z, extent = get_demo_image()\n\n im = ax.imshow(Z, extent=extent)\n plt.colorbar(im, cax=ax_cb)\n ax_cb.yaxis.set_tick_params(labelright=False)\n\n\ndef demo_locatable_axes_easy(ax):\n from mpl_toolkits.axes_grid1 import make_axes_locatable\n\n divider = make_axes_locatable(ax)\n\n ax_cb = divider.append_axes(\"right\", size=\"5%\", pad=0.05)\n fig = ax.get_figure()\n fig.add_axes(ax_cb)\n\n Z, extent = get_demo_image()\n im = ax.imshow(Z, extent=extent)\n\n plt.colorbar(im, cax=ax_cb)\n ax_cb.yaxis.tick_right()\n ax_cb.yaxis.set_tick_params(labelright=False)\n\n\ndef demo_images_side_by_side(ax):\n from mpl_toolkits.axes_grid1 import make_axes_locatable\n\n divider = make_axes_locatable(ax)\n\n Z, extent = get_demo_image()\n ax2 = divider.append_axes(\"right\", size=\"100%\", pad=0.05)\n fig1 = ax.get_figure()\n fig1.add_axes(ax2)\n\n ax.imshow(Z, extent=extent)\n ax2.imshow(Z, extent=extent)\n ax2.yaxis.set_tick_params(labelleft=False)\n\n\ndef demo():\n fig = plt.figure(figsize=(6, 6))\n\n # PLOT 1\n # simple image & colorbar\n ax = fig.add_subplot(2, 2, 1)\n demo_simple_image(ax)\n\n # PLOT 2\n # image and colorbar with draw-time positioning -- a hard way\n demo_locatable_axes_hard(fig)\n\n # PLOT 3\n # image and colorbar with draw-time positioning -- an easy way\n ax = fig.add_subplot(2, 2, 3)\n demo_locatable_axes_easy(ax)\n\n # PLOT 4\n # two images side by side with fixed padding.\n ax = fig.add_subplot(2, 2, 4)\n demo_images_side_by_side(ax)\n\n plt.show()\n\n\ndemo()" ] } ], "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 }