|
| 1 | +.. _annotations-tutorial: |
| 2 | + |
| 3 | +Annotating text |
| 4 | +=============== |
| 5 | + |
| 6 | +The uses of the basic :func:`~matplotlib.pyplot.text` command above |
| 7 | +place text at an arbitrary position on the Axes. A common use case of |
| 8 | +text is to annotate some feature of the plot, and the |
| 9 | +:func:`~matplotlib.Axes.annotate` method provides helper functionality |
| 10 | +to make annotations easy. In an annotation, there are two points to |
| 11 | +consider: the location being annotated represented by the argument |
| 12 | +``xy`` and the location of the text ``xytext``. Both of these |
| 13 | +arguments are ``(x,y)`` tuples. |
| 14 | + |
| 15 | +.. literalinclude:: figures/annotation_basic.py |
| 16 | + |
| 17 | +.. image:: figures/annotation_basic.png |
| 18 | + :scale: 50 |
| 19 | + |
| 20 | +In this example, both the ``xy`` (arrow tip) and ``xytext`` locations |
| 21 | +(text location) are in data coordinates. There are a variety of other |
| 22 | +coordinate systems one can choose -- you can specify the coordinate |
| 23 | +system of ``xy`` and ``xytext`` with one of the following strings for |
| 24 | +``xycoords`` and ``textcoords`` (default is 'data') |
| 25 | + |
| 26 | +==================== ==================================================== |
| 27 | +argument coordinate system |
| 28 | +==================== ==================================================== |
| 29 | + 'figure points' points from the lower left corner of the figure |
| 30 | + 'figure pixels' pixels from the lower left corner of the figure |
| 31 | + 'figure fraction' 0,0 is lower left of figure and 1,1 is upper, right |
| 32 | + 'axes points' points from lower left corner of axes |
| 33 | + 'axes pixels' pixels from lower left corner of axes |
| 34 | + 'axes fraction' 0,1 is lower left of axes and 1,1 is upper right |
| 35 | + 'data' use the axes data coordinate system |
| 36 | +==================== ==================================================== |
| 37 | + |
| 38 | +For example to place the text coordinates in fractional axes |
| 39 | +coordinates, one could do:: |
| 40 | + |
| 41 | + ax.annotate('local max', xy=(3, 1), xycoords='data', |
| 42 | + xytext=(0.8, 0.95), textcoords='axes fraction', |
| 43 | + arrowprops=dict(facecolor='black', shrink=0.05), |
| 44 | + horizontalalignment='right', verticalalignment='top', |
| 45 | + ) |
| 46 | + |
| 47 | +For physical coordinate systems (points or pixels) the origin is the |
| 48 | +(bottom, left) of the figure or axes. If the value is negative, |
| 49 | +however, the origin is from the (right, top) of the figure or axes, |
| 50 | +analogous to negative indexing of sequences. |
| 51 | + |
| 52 | +Optionally, you can specify arrow properties which draws an arrow |
| 53 | +from the text to the annotated point by giving a dictionary of arrow |
| 54 | +properties in the optional keyword argument ``arrowprops``. |
| 55 | + |
| 56 | + |
| 57 | +==================== =========================================================================== |
| 58 | +``arrowprops`` key description |
| 59 | +==================== =========================================================================== |
| 60 | +width the width of the arrow in points |
| 61 | +frac the fraction of the arrow length occupied by the head |
| 62 | +headwidth the width of the base of the arrow head in points |
| 63 | +shrink move the tip and base some percent away from the annotated point and text |
| 64 | +\*\*kwargs any key for :class:`matplotlib.patches.Polygon`, eg ``facecolor`` |
| 65 | +==================== =========================================================================== |
| 66 | + |
| 67 | + |
| 68 | +In the example below, the ``xy`` point is in native coordinates |
| 69 | +(``xycoords`` defaults to 'data'). For a polar axes, this is in |
| 70 | +(theta, radius) space. The text in this example is placed in the |
| 71 | +fractional figure coordinate system. :class:`matplotlib.text.Text` |
| 72 | +keyword args like ``horizontalalignment``, ``verticalalignment`` and |
| 73 | +``fontsize are passed from the `~matplotlib.Axes.annotate` to the |
| 74 | +``Text`` instance |
| 75 | + |
| 76 | +.. literalinclude:: figures/annotation_polar.py |
| 77 | + |
| 78 | +.. image:: figures/annotation_polar.png |
| 79 | + :scale: 50 |
| 80 | + |
| 81 | +See the `annotations demo |
| 82 | +<http://matplotlib.sf.net/examples/pylab_examples/annotation_demo.py>`_ for more |
| 83 | +examples. |
0 commit comments