{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Annotate plots\n\nThe following examples show ways to annotate plots in Matplotlib.\nThis includes highlighting specific points of interest and using various\nvisual tools to call attention to this point. For a more complete and in-depth\ndescription of the annotation and text tools in Matplotlib, see the\n`tutorial on annotation `.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.patches import Ellipse\nfrom matplotlib.text import OffsetFrom" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Specifying text points and annotation points\n\nYou must specify an annotation point ``xy=(x, y)`` to annotate this point.\nAdditionally, you may specify a text point ``xytext=(x, y)`` for the location\nof the text for this annotation. Optionally, you can specify the coordinate\nsystem of *xy* and *xytext* with one of the following strings for *xycoords*\nand *textcoords* (default is 'data')::\n\n 'figure points' : points from the lower left corner of the figure\n 'figure pixels' : pixels from the lower left corner of the figure\n 'figure fraction' : (0, 0) is lower left of figure and (1, 1) is upper right\n 'axes points' : points from lower left corner of the Axes\n 'axes pixels' : pixels from lower left corner of the Axes\n 'axes fraction' : (0, 0) is lower left of Axes and (1, 1) is upper right\n 'offset points' : Specify an offset (in points) from the xy value\n 'offset pixels' : Specify an offset (in pixels) from the xy value\n 'data' : use the Axes data coordinate system\n\nNote: for physical coordinate systems (points or pixels) the origin is the\n(bottom, left) of the figure or Axes.\n\nOptionally, you can specify arrow properties which draws and arrow\nfrom the text to the annotated point by giving a dictionary of arrow\nproperties\n\nValid keys are::\n\n width : the width of the arrow in points\n frac : the fraction of the arrow length occupied by the head\n headwidth : the width of the base of the arrow head in points\n shrink : move the tip and base some percent away from the\n annotated point and text\n any key for matplotlib.patches.polygon (e.g., facecolor)\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Create our figure and data we'll use for plotting\nfig, ax = plt.subplots(figsize=(4, 4))\n\nt = np.arange(0.0, 5.0, 0.01)\ns = np.cos(2*np.pi*t)\n\n# Plot a line and add some simple annotations\nline, = ax.plot(t, s)\nax.annotate('figure pixels',\n xy=(10, 10), xycoords='figure pixels')\nax.annotate('figure points',\n xy=(107, 110), xycoords='figure points',\n fontsize=12)\nax.annotate('figure fraction',\n xy=(.025, .975), xycoords='figure fraction',\n horizontalalignment='left', verticalalignment='top',\n fontsize=20)\n\n# The following examples show off how these arrows are drawn.\n\nax.annotate('point offset from data',\n xy=(3, 1), xycoords='data',\n xytext=(-10, 90), textcoords='offset points',\n arrowprops=dict(facecolor='black', shrink=0.05),\n horizontalalignment='center', verticalalignment='bottom')\n\nax.annotate('axes fraction',\n xy=(2, 1), xycoords='data',\n xytext=(0.36, 0.68), textcoords='axes fraction',\n arrowprops=dict(facecolor='black', shrink=0.05),\n horizontalalignment='right', verticalalignment='top')\n\n# You may also use negative points or pixels to specify from (right, top).\n# E.g., (-10, 10) is 10 points to the left of the right side of the Axes and 10\n# points above the bottom\n\nax.annotate('pixel offset from axes fraction',\n xy=(1, 0), xycoords='axes fraction',\n xytext=(-20, 20), textcoords='offset pixels',\n horizontalalignment='right',\n verticalalignment='bottom')\n\nax.set(xlim=(-1, 5), ylim=(-3, 5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using multiple coordinate systems and axis types\n\nYou can specify the *xypoint* and the *xytext* in different positions and\ncoordinate systems, and optionally turn on a connecting line and mark the\npoint with a marker. Annotations work on polar Axes too.\n\nIn the example below, the *xy* point is in native coordinates (*xycoords*\ndefaults to 'data'). For a polar Axes, this is in (theta, radius) space.\nThe text in the example is placed in the fractional figure coordinate system.\nText keyword arguments like horizontal and vertical alignment are respected.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(subplot_kw=dict(projection='polar'), figsize=(3, 3))\nr = np.arange(0, 1, 0.001)\ntheta = 2*2*np.pi*r\nline, = ax.plot(theta, r)\n\nind = 800\nthisr, thistheta = r[ind], theta[ind]\nax.plot([thistheta], [thisr], 'o')\nax.annotate('a polar annotation',\n xy=(thistheta, thisr), # theta, radius\n xytext=(0.05, 0.05), # fraction, fraction\n textcoords='figure fraction',\n arrowprops=dict(facecolor='black', shrink=0.05),\n horizontalalignment='left',\n verticalalignment='bottom')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also use polar notation on a cartesian Axes. Here the native\ncoordinate system ('data') is cartesian, so you need to specify the\nxycoords and textcoords as 'polar' if you want to use (theta, radius).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "el = Ellipse((0, 0), 10, 20, facecolor='r', alpha=0.5)\n\nfig, ax = plt.subplots(subplot_kw=dict(aspect='equal'))\nax.add_artist(el)\nel.set_clip_box(ax.bbox)\nax.annotate('the top',\n xy=(np.pi/2., 10.), # theta, radius\n xytext=(np.pi/3, 20.), # theta, radius\n xycoords='polar',\n textcoords='polar',\n arrowprops=dict(facecolor='black', shrink=0.05),\n horizontalalignment='left',\n verticalalignment='bottom',\n clip_on=True) # clip to the Axes bounding box\n\nax.set(xlim=[-20, 20], ylim=[-20, 20])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Customizing arrow and bubble styles\n\nThe arrow between *xytext* and the annotation point, as well as the bubble\nthat covers the annotation text, are highly customizable. Below are a few\nparameter options as well as their resulting output.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(8, 5))\n\nt = np.arange(0.0, 5.0, 0.01)\ns = np.cos(2*np.pi*t)\nline, = ax.plot(t, s, lw=3)\n\nax.annotate(\n 'straight',\n xy=(0, 1), xycoords='data',\n xytext=(-50, 30), textcoords='offset points',\n arrowprops=dict(arrowstyle=\"->\"))\nax.annotate(\n 'arc3,\\nrad 0.2',\n xy=(0.5, -1), xycoords='data',\n xytext=(-80, -60), textcoords='offset points',\n arrowprops=dict(arrowstyle=\"->\",\n connectionstyle=\"arc3,rad=.2\"))\nax.annotate(\n 'arc,\\nangle 50',\n xy=(1., 1), xycoords='data',\n xytext=(-90, 50), textcoords='offset points',\n arrowprops=dict(arrowstyle=\"->\",\n connectionstyle=\"arc,angleA=0,armA=50,rad=10\"))\nax.annotate(\n 'arc,\\narms',\n xy=(1.5, -1), xycoords='data',\n xytext=(-80, -60), textcoords='offset points',\n arrowprops=dict(\n arrowstyle=\"->\",\n connectionstyle=\"arc,angleA=0,armA=40,angleB=-90,armB=30,rad=7\"))\nax.annotate(\n 'angle,\\nangle 90',\n xy=(2., 1), xycoords='data',\n xytext=(-70, 30), textcoords='offset points',\n arrowprops=dict(arrowstyle=\"->\",\n connectionstyle=\"angle,angleA=0,angleB=90,rad=10\"))\nax.annotate(\n 'angle3,\\nangle -90',\n xy=(2.5, -1), xycoords='data',\n xytext=(-80, -60), textcoords='offset points',\n arrowprops=dict(arrowstyle=\"->\",\n connectionstyle=\"angle3,angleA=0,angleB=-90\"))\nax.annotate(\n 'angle,\\nround',\n xy=(3., 1), xycoords='data',\n xytext=(-60, 30), textcoords='offset points',\n bbox=dict(boxstyle=\"round\", fc=\"0.8\"),\n arrowprops=dict(arrowstyle=\"->\",\n connectionstyle=\"angle,angleA=0,angleB=90,rad=10\"))\nax.annotate(\n 'angle,\\nround4',\n xy=(3.5, -1), xycoords='data',\n xytext=(-70, -80), textcoords='offset points',\n size=20,\n bbox=dict(boxstyle=\"round4,pad=.5\", fc=\"0.8\"),\n arrowprops=dict(arrowstyle=\"->\",\n connectionstyle=\"angle,angleA=0,angleB=-90,rad=10\"))\nax.annotate(\n 'angle,\\nshrink',\n xy=(4., 1), xycoords='data',\n xytext=(-60, 30), textcoords='offset points',\n bbox=dict(boxstyle=\"round\", fc=\"0.8\"),\n arrowprops=dict(arrowstyle=\"->\",\n shrinkA=0, shrinkB=10,\n connectionstyle=\"angle,angleA=0,angleB=90,rad=10\"))\n# You can pass an empty string to get only annotation arrows rendered\nax.annotate('', xy=(4., 1.), xycoords='data',\n xytext=(4.5, -1), textcoords='data',\n arrowprops=dict(arrowstyle=\"<->\",\n connectionstyle=\"bar\",\n ec=\"k\",\n shrinkA=5, shrinkB=5))\n\nax.set(xlim=(-1, 5), ylim=(-4, 3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We'll create another figure so that it doesn't get too cluttered\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n\nel = Ellipse((2, -1), 0.5, 0.5)\nax.add_patch(el)\n\nax.annotate('$->$',\n xy=(2., -1), xycoords='data',\n xytext=(-150, -140), textcoords='offset points',\n bbox=dict(boxstyle=\"round\", fc=\"0.8\"),\n arrowprops=dict(arrowstyle=\"->\",\n patchB=el,\n connectionstyle=\"angle,angleA=90,angleB=0,rad=10\"))\nax.annotate('arrow\\nfancy',\n xy=(2., -1), xycoords='data',\n xytext=(-100, 60), textcoords='offset points',\n size=20,\n arrowprops=dict(arrowstyle=\"fancy\",\n fc=\"0.6\", ec=\"none\",\n patchB=el,\n connectionstyle=\"angle3,angleA=0,angleB=-90\"))\nax.annotate('arrow\\nsimple',\n xy=(2., -1), xycoords='data',\n xytext=(100, 60), textcoords='offset points',\n size=20,\n arrowprops=dict(arrowstyle=\"simple\",\n fc=\"0.6\", ec=\"none\",\n patchB=el,\n connectionstyle=\"arc3,rad=0.3\"))\nax.annotate('wedge',\n xy=(2., -1), xycoords='data',\n xytext=(-100, -100), textcoords='offset points',\n size=20,\n arrowprops=dict(arrowstyle=\"wedge,tail_width=0.7\",\n fc=\"0.6\", ec=\"none\",\n patchB=el,\n connectionstyle=\"arc3,rad=-0.3\"))\nax.annotate('bubble,\\ncontours',\n xy=(2., -1), xycoords='data',\n xytext=(0, -70), textcoords='offset points',\n size=20,\n bbox=dict(boxstyle=\"round\",\n fc=(1.0, 0.7, 0.7),\n ec=(1., .5, .5)),\n arrowprops=dict(arrowstyle=\"wedge,tail_width=1.\",\n fc=(1.0, 0.7, 0.7), ec=(1., .5, .5),\n patchA=None,\n patchB=el,\n relpos=(0.2, 0.8),\n connectionstyle=\"arc3,rad=-0.1\"))\nax.annotate('bubble',\n xy=(2., -1), xycoords='data',\n xytext=(55, 0), textcoords='offset points',\n size=20, va=\"center\",\n bbox=dict(boxstyle=\"round\", fc=(1.0, 0.7, 0.7), ec=\"none\"),\n arrowprops=dict(arrowstyle=\"wedge,tail_width=1.\",\n fc=(1.0, 0.7, 0.7), ec=\"none\",\n patchA=None,\n patchB=el,\n relpos=(0.2, 0.5)))\n\nax.set(xlim=(-1, 5), ylim=(-5, 3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More examples of coordinate systems\n\nBelow we'll show a few more examples of coordinate systems and how the\nlocation of annotations may be specified.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, (ax1, ax2) = plt.subplots(1, 2)\n\nbbox_args = dict(boxstyle=\"round\", fc=\"0.8\")\narrow_args = dict(arrowstyle=\"->\")\n\n# Here we'll demonstrate the extents of the coordinate system and how\n# we place annotating text.\n\nax1.annotate('figure fraction : 0, 0', xy=(0, 0), xycoords='figure fraction',\n xytext=(20, 20), textcoords='offset points',\n ha=\"left\", va=\"bottom\",\n bbox=bbox_args,\n arrowprops=arrow_args)\n\nax1.annotate('figure fraction : 1, 1', xy=(1, 1), xycoords='figure fraction',\n xytext=(-20, -20), textcoords='offset points',\n ha=\"right\", va=\"top\",\n bbox=bbox_args,\n arrowprops=arrow_args)\n\nax1.annotate('axes fraction : 0, 0', xy=(0, 0), xycoords='axes fraction',\n xytext=(20, 20), textcoords='offset points',\n ha=\"left\", va=\"bottom\",\n bbox=bbox_args,\n arrowprops=arrow_args)\n\nax1.annotate('axes fraction : 1, 1', xy=(1, 1), xycoords='axes fraction',\n xytext=(-20, -20), textcoords='offset points',\n ha=\"right\", va=\"top\",\n bbox=bbox_args,\n arrowprops=arrow_args)\n\n# It is also possible to generate draggable annotations\n\nan1 = ax1.annotate('Drag me 1', xy=(.5, .7), xycoords='data',\n ha=\"center\", va=\"center\",\n bbox=bbox_args)\n\nan2 = ax1.annotate('Drag me 2', xy=(.5, .5), xycoords=an1,\n xytext=(.5, .3), textcoords='axes fraction',\n ha=\"center\", va=\"center\",\n bbox=bbox_args,\n arrowprops=dict(patchB=an1.get_bbox_patch(),\n connectionstyle=\"arc3,rad=0.2\",\n **arrow_args))\nan1.draggable()\nan2.draggable()\n\nan3 = ax1.annotate('', xy=(.5, .5), xycoords=an2,\n xytext=(.5, .5), textcoords=an1,\n ha=\"center\", va=\"center\",\n bbox=bbox_args,\n arrowprops=dict(patchA=an1.get_bbox_patch(),\n patchB=an2.get_bbox_patch(),\n connectionstyle=\"arc3,rad=0.2\",\n **arrow_args))\n\n# Finally we'll show off some more complex annotation and placement\n\ntext = ax2.annotate('xy=(0, 1)\\nxycoords=(\"data\", \"axes fraction\")',\n xy=(0, 1), xycoords=(\"data\", 'axes fraction'),\n xytext=(0, -20), textcoords='offset points',\n ha=\"center\", va=\"top\",\n bbox=bbox_args,\n arrowprops=arrow_args)\n\nax2.annotate('xy=(0.5, 0)\\nxycoords=artist',\n xy=(0.5, 0.), xycoords=text,\n xytext=(0, -20), textcoords='offset points',\n ha=\"center\", va=\"top\",\n bbox=bbox_args,\n arrowprops=arrow_args)\n\nax2.annotate('xy=(0.8, 0.5)\\nxycoords=ax1.transData',\n xy=(0.8, 0.5), xycoords=ax1.transData,\n xytext=(10, 10),\n textcoords=OffsetFrom(ax2.bbox, (0, 0), \"points\"),\n ha=\"left\", va=\"bottom\",\n bbox=bbox_args,\n arrowprops=arrow_args)\n\nax2.set(xlim=[-2, 2], ylim=[-2, 2])\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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }