{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# AnnotationBbox demo\n\n`.AnnotationBbox` creates an annotation using an `.OffsetBox`, and\nprovides more fine-grained control than `.Axes.annotate`. This example\ndemonstrates the use of AnnotationBbox together with three different\nOffsetBoxes: `.TextArea`, `.DrawingArea`, and `.OffsetImage`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.cbook import get_sample_data\nfrom matplotlib.offsetbox import (AnnotationBbox, DrawingArea, OffsetImage,\n TextArea)\nfrom matplotlib.patches import Circle\n\nfig, ax = plt.subplots()\n\n# Define a 1st position to annotate (display it with a marker)\nxy = (0.5, 0.7)\nax.plot(xy[0], xy[1], \".r\")\n\n# Annotate the 1st position with a text box ('Test 1')\noffsetbox = TextArea(\"Test 1\")\n\nab = AnnotationBbox(offsetbox, xy,\n xybox=(-20, 40),\n xycoords='data',\n boxcoords=\"offset points\",\n arrowprops=dict(arrowstyle=\"->\"),\n bboxprops=dict(boxstyle=\"sawtooth\"))\nax.add_artist(ab)\n\n# Annotate the 1st position with another text box ('Test')\noffsetbox = TextArea(\"Test\")\n\nab = AnnotationBbox(offsetbox, xy,\n xybox=(1.02, xy[1]),\n xycoords='data',\n boxcoords=(\"axes fraction\", \"data\"),\n box_alignment=(0., 0.5),\n arrowprops=dict(arrowstyle=\"->\"))\nax.add_artist(ab)\n\n# Define a 2nd position to annotate (don't display with a marker this time)\nxy = [0.3, 0.55]\n\n# Annotate the 2nd position with a circle patch\nda = DrawingArea(20, 20, 0, 0)\np = Circle((10, 10), 10)\nda.add_artist(p)\n\nab = AnnotationBbox(da, xy,\n xybox=(1., xy[1]),\n xycoords='data',\n boxcoords=(\"axes fraction\", \"data\"),\n box_alignment=(0.2, 0.5),\n arrowprops=dict(arrowstyle=\"->\"),\n bboxprops=dict(alpha=0.5))\n\nax.add_artist(ab)\n\n# Annotate the 2nd position with an image (a generated array of pixels)\narr = np.arange(100).reshape((10, 10))\nim = OffsetImage(arr, zoom=2)\nim.image.axes = ax\n\nab = AnnotationBbox(im, xy,\n xybox=(-50., 50.),\n xycoords='data',\n boxcoords=\"offset points\",\n pad=0.3,\n arrowprops=dict(arrowstyle=\"->\"))\n\nax.add_artist(ab)\n\n# Annotate the 2nd position with another image (a Grace Hopper portrait)\nwith get_sample_data(\"grace_hopper.jpg\") as file:\n arr_img = plt.imread(file)\n\nimagebox = OffsetImage(arr_img, zoom=0.2)\nimagebox.image.axes = ax\n\nab = AnnotationBbox(imagebox, xy,\n xybox=(120., -80.),\n xycoords='data',\n boxcoords=\"offset points\",\n pad=0.5,\n arrowprops=dict(\n arrowstyle=\"->\",\n connectionstyle=\"angle,angleA=0,angleB=90,rad=3\")\n )\n\nax.add_artist(ab)\n\n# Fix the display limits to see everything\nax.set_xlim(0, 1)\nax.set_ylim(0, 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.patches.Circle`\n - `matplotlib.offsetbox.TextArea`\n - `matplotlib.offsetbox.DrawingArea`\n - `matplotlib.offsetbox.OffsetImage`\n - `matplotlib.offsetbox.AnnotationBbox`\n - `matplotlib.cbook.get_sample_data`\n - `matplotlib.pyplot.subplots`\n - `matplotlib.pyplot.imread`\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 }