{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Bar chart with gradients\n\nMatplotlib does not natively support gradients. However, we can emulate a\ngradient-filled rectangle by an `.AxesImage` of the right size and coloring.\n\nIn particular, we use a colormap to generate the actual colors. It is then\nsufficient to define the underlying values on the corners of the image and\nlet bicubic interpolation fill out the area. We define the gradient direction\nby a unit vector *v*. The values at the corners are then obtained by the\nlengths of the projections of the corner vectors on *v*.\n\nA similar approach can be used to create a gradient background for an Axes.\nIn that case, it is helpful to use Axes coordinates (``extent=(0, 1, 0, 1),\ntransform=ax.transAxes``) to be independent of the data coordinates.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nnp.random.seed(19680801)\n\n\ndef gradient_image(ax, direction=0.3, cmap_range=(0, 1), **kwargs):\n    \"\"\"\n    Draw a gradient image based on a colormap.\n\n    Parameters\n    ----------\n    ax : Axes\n        The Axes to draw on.\n    direction : float\n        The direction of the gradient. This is a number in\n        range 0 (=vertical) to 1 (=horizontal).\n    cmap_range : float, float\n        The fraction (cmin, cmax) of the colormap that should be\n        used for the gradient, where the complete colormap is (0, 1).\n    **kwargs\n        Other parameters are passed on to `.Axes.imshow()`.\n        In particular, *cmap*, *extent*, and *transform* may be useful.\n    \"\"\"\n    phi = direction * np.pi / 2\n    v = np.array([np.cos(phi), np.sin(phi)])\n    X = np.array([[v @ [1, 0], v @ [1, 1]],\n                  [v @ [0, 0], v @ [0, 1]]])\n    a, b = cmap_range\n    X = a + (b - a) / X.max() * X\n    im = ax.imshow(X, interpolation='bicubic', clim=(0, 1),\n                   aspect='auto', **kwargs)\n    return im\n\n\ndef gradient_bar(ax, x, y, width=0.5, bottom=0):\n    for left, top in zip(x, y):\n        right = left + width\n        gradient_image(ax, extent=(left, right, bottom, top),\n                       cmap=\"Blues_r\", cmap_range=(0, 0.8))\n\n\nfig, ax = plt.subplots()\nax.set(xlim=(0, 10), ylim=(0, 1))\n\n# background image\ngradient_image(ax, direction=1, extent=(0, 1, 0, 1), transform=ax.transAxes,\n               cmap=\"RdYlGn\", cmap_range=(0.2, 0.8), alpha=0.5)\n\nN = 10\nx = np.arange(N) + 0.15\ny = np.random.rand(N)\ngradient_bar(ax, x, y, width=0.7)\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. tags::\n\n   styling: color\n   plot-type: imshow\n   level: intermediate\n   purpose: showcase\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
}