{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Per-row or per-column colorbars\n\nThis example shows how to use one common colorbar for each row or column\nof an image grid.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nfrom matplotlib import cbook\nfrom mpl_toolkits.axes_grid1 import AxesGrid\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_bottom_cbar(fig):\n \"\"\"\n A grid of 2x2 images with a colorbar for each column.\n \"\"\"\n grid = AxesGrid(fig, 121, # similar to subplot(121)\n nrows_ncols=(2, 2),\n axes_pad=0.10,\n share_all=True,\n label_mode=\"1\",\n cbar_location=\"bottom\",\n cbar_mode=\"edge\",\n cbar_pad=0.25,\n cbar_size=\"15%\",\n direction=\"column\"\n )\n\n Z, extent = get_demo_image()\n cmaps = [\"autumn\", \"summer\"]\n for i in range(4):\n im = grid[i].imshow(Z, extent=extent, cmap=cmaps[i//2])\n if i % 2:\n grid.cbar_axes[i//2].colorbar(im)\n\n for cax in grid.cbar_axes:\n cax.axis[cax.orientation].set_label(\"Bar\")\n\n # This affects all Axes as share_all = True.\n grid.axes_llc.set_xticks([-2, 0, 2])\n grid.axes_llc.set_yticks([-2, 0, 2])\n\n\ndef demo_right_cbar(fig):\n \"\"\"\n A grid of 2x2 images. Each row has its own colorbar.\n \"\"\"\n grid = AxesGrid(fig, 122, # similar to subplot(122)\n nrows_ncols=(2, 2),\n axes_pad=0.10,\n label_mode=\"1\",\n share_all=True,\n cbar_location=\"right\",\n cbar_mode=\"edge\",\n cbar_size=\"7%\",\n cbar_pad=\"2%\",\n )\n Z, extent = get_demo_image()\n cmaps = [\"spring\", \"winter\"]\n for i in range(4):\n im = grid[i].imshow(Z, extent=extent, cmap=cmaps[i//2])\n if i % 2:\n grid.cbar_axes[i//2].colorbar(im)\n\n for cax in grid.cbar_axes:\n cax.axis[cax.orientation].set_label('Foo')\n\n # This affects all Axes because we set share_all = True.\n grid.axes_llc.set_xticks([-2, 0, 2])\n grid.axes_llc.set_yticks([-2, 0, 2])\n\n\nfig = plt.figure()\n\ndemo_bottom_cbar(fig)\ndemo_right_cbar(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.5" } }, "nbformat": 4, "nbformat_minor": 0 }