{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Colorbar tick labelling\n\nProduce custom labelling for a colorbar.\n\nContributed by Scott Sinclair\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib import cm\nfrom numpy.random import randn\n\n# Make plot with vertical (default) colorbar\nfig, ax = plt.subplots()\n\ndata = np.clip(randn(250, 250), -1, 1)\n\ncax = ax.imshow(data, interpolation=\"nearest\", cmap=cm.coolwarm)\nax.set_title(\"Gaussian noise with vertical colorbar\")\n\n# Add colorbar, make sure to specify tick locations to match desired ticklabels\ncbar = fig.colorbar(cax, ticks=[-1, 0, 1])\ncbar.ax.set_yticklabels([\"< -1\", \"0\", \"> 1\"]) # vertically oriented colorbar\n\n# Make plot with horizontal colorbar\nfig, ax = plt.subplots()\n\ndata = np.clip(randn(250, 250), -1, 1)\n\ncax = ax.imshow(data, interpolation=\"nearest\", cmap=cm.afmhot)\nax.set_title(\"Gaussian noise with horizontal colorbar\")\n\ncbar = fig.colorbar(cax, ticks=[-1, 0, 1], orientation=\"horizontal\")\ncbar.ax.set_xticklabels([\"Low\", \"Medium\", \"High\"]) # horizontal colorbar\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.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }