{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Colorbar Tick labelling\n\nVertical colorbars have ticks, tick labels, and labels visible on the *y* axis,\nhorizontal colorbars on the *x* axis. The ``ticks`` parameter can be used to\nset the ticks and the ``format`` parameter can be used to format the tick labels\nof the visible colorbar Axes. For further adjustments, the ``yaxis`` or\n``xaxis`` Axes of the colorbar can be retrieved using its ``ax`` property.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.ticker as mticker\n\n# Fixing random state for reproducibility\nrng = np.random.default_rng(seed=19680801)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Make plot with vertical (default) colorbar\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots()\n\ndata = rng.standard_normal((250, 250))\n\ncax = ax.imshow(data, vmin=-1, vmax=1, cmap='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,\n                    ticks=[-1, 0, 1],\n                    format=mticker.FixedFormatter(['< -1', '0', '> 1']),\n                    extend='both'\n                    )\nlabels = cbar.ax.get_yticklabels()\nlabels[0].set_verticalalignment('top')\nlabels[-1].set_verticalalignment('bottom')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Make plot with horizontal colorbar\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots()\n\ndata = np.clip(data, -1, 1)\n\ncax = ax.imshow(data, cmap='afmhot')\nax.set_title('Gaussian noise with horizontal colorbar')\n\n# Add colorbar and adjust ticks afterwards\ncbar = fig.colorbar(cax, orientation='horizontal')\ncbar.set_ticks(ticks=[-1, 0, 1], labels=['Low', 'Medium', 'High'])\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n   The use of the following functions, methods, classes and modules is shown\n   in this example:\n\n   - `matplotlib.colorbar.Colorbar.set_ticks`\n   - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`\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.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}