{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# BboxImage Demo\n\nA `~matplotlib.image.BboxImage` can be used to position an image according to\na bounding box. This demo shows how to show an image inside a `.text.Text`'s\nbounding box as well as how to manually create a bounding box for the image.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.image import BboxImage\nfrom matplotlib.transforms import Bbox, TransformedBbox\n\nfig, (ax1, ax2) = plt.subplots(ncols=2)\n\n# ----------------------------\n# Create a BboxImage with Text\n# ----------------------------\ntxt = ax1.text(0.5, 0.5, \"test\", size=30, ha=\"center\", color=\"w\")\nax1.add_artist(\n    BboxImage(txt.get_window_extent, data=np.arange(256).reshape((1, -1))))\n\n# ------------------------------------\n# Create a BboxImage for each colormap\n# ------------------------------------\n# List of all colormaps; skip reversed colormaps.\ncmap_names = sorted(m for m in plt.colormaps if not m.endswith(\"_r\"))\n\nncol = 2\nnrow = len(cmap_names) // ncol + 1\n\nxpad_fraction = 0.3\ndx = 1 / (ncol + xpad_fraction * (ncol - 1))\n\nypad_fraction = 0.3\ndy = 1 / (nrow + ypad_fraction * (nrow - 1))\n\nfor i, cmap_name in enumerate(cmap_names):\n    ix, iy = divmod(i, nrow)\n    bbox0 = Bbox.from_bounds(ix*dx*(1+xpad_fraction),\n                             1 - iy*dy*(1+ypad_fraction) - dy,\n                             dx, dy)\n    bbox = TransformedBbox(bbox0, ax2.transAxes)\n    ax2.add_artist(\n        BboxImage(bbox, cmap=cmap_name, data=np.arange(256).reshape((1, -1))))\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.image.BboxImage`\n   - `matplotlib.transforms.Bbox`\n   - `matplotlib.transforms.TransformedBbox`\n   - `matplotlib.text.Text`\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
}