{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Multiple images with one colorbar\n\nUse a single colorbar for multiple images.\n\nCurrently, a colorbar can only be connected to one image. The connection\nguarantees that the data coloring is consistent with the colormap scale\n(i.e. the color of value *x* in the colormap is used for coloring a data\nvalue *x* in the image).\n\nIf we want one colorbar to be representative for multiple images, we have\nto explicitly ensure consistent data coloring by using the same data\nnormalization for all the images. We ensure this by explicitly creating a\n``norm`` object that we pass to all the image plotting methods.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib import colors\n\nnp.random.seed(19680801)\n\ndatasets = [\n (i+1)/10 * np.random.rand(10, 20)\n for i in range(4)\n]\n\nfig, axs = plt.subplots(2, 2)\nfig.suptitle('Multiple images')\n\n# create a single norm to be shared across all images\nnorm = colors.Normalize(vmin=np.min(datasets), vmax=np.max(datasets))\n\nimages = []\nfor ax, data in zip(axs.flat, datasets):\n images.append(ax.imshow(data, norm=norm))\n\nfig.colorbar(images[0], ax=axs, orientation='horizontal', fraction=.1)\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The colors are now kept consistent across all images when changing the\nscaling, e.g. through zooming in the colorbar or via the \"edit axis,\ncurves and images parameters\" GUI of the Qt backend. This is sufficient\nfor most practical use cases.\n\n## Advanced: Additionally sync the colormap\n\nSharing a common norm object guarantees synchronized scaling because scale\nchanges modify the norm object in-place and thus propagate to all images\nthat use this norm. This approach does not help with synchronizing colormaps\nbecause changing the colormap of an image (e.g. through the \"edit axis,\ncurves and images parameters\" GUI of the Qt backend) results in the image\nreferencing the new colormap object. Thus, the other images are not updated.\n\nTo update the other images, sync the\ncolormaps using the following code::\n\n def sync_cmaps(changed_image):\n for im in images:\n if changed_image.get_cmap() != im.get_cmap():\n im.set_cmap(changed_image.get_cmap())\n\n for im in images:\n im.callbacks.connect('changed', sync_cmaps)\n\n\n.. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.imshow` / `matplotlib.pyplot.imshow`\n - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`\n - `matplotlib.colors.Normalize`\n - `matplotlib.cm.ScalarMappable.set_cmap`\n - `matplotlib.cbook.CallbackRegistry.connect`\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 }