matplotlib.axes.Axes.annotate#

Axes.annotate(text,xy,xytext=None,xycoords='data',textcoords=None,arrowprops=None,annotation_clip=None,**kwargs)[source]#

Annotate the pointxy with texttext.

In the simplest form, the text is placed atxy.

Optionally, the text can be displayed in another positionxytext.An arrow pointing from the text to the annotated pointxy can thenbe added by definingarrowprops.

Parameters:
textstr

The text of the annotation.

xy(float, float)

The point(x, y) to annotate. The coordinate system is determinedbyxycoords.

xytext(float, float), default:xy

The position(x, y) to place the text at. The coordinate systemis determined bytextcoords.

xycoordssingle or two-tuple of str orArtist orTransform or callable, default: 'data'

The coordinate system thatxy is given in. The following typesof values are supported:

  • One of the following strings:

    Value

    Description

    'figure points'

    Points from the lower left of the figure

    'figure pixels'

    Pixels from the lower left of the figure

    'figure fraction'

    Fraction of figure from lower left

    'subfigure points'

    Points from the lower left of the subfigure

    'subfigure pixels'

    Pixels from the lower left of the subfigure

    'subfigure fraction'

    Fraction of subfigure from lower left

    'axes points'

    Points from lower left corner of the Axes

    'axes pixels'

    Pixels from lower left corner of the Axes

    'axes fraction'

    Fraction of Axes from lower left

    'data'

    Use the coordinate system of the objectbeing annotated (default)

    'polar'

    (theta, r) if not native 'data'coordinates

    Note that 'subfigure pixels' and 'figure pixels' are the samefor the parent figure, so users who want code that is usable ina subfigure can use 'subfigure pixels'.

  • AnArtist:xy is interpreted as a fraction of the artist'sBbox. E.g.(0, 0) would be the lowerleft corner of the bounding box and(0.5, 1) would be thecenter top of the bounding box.

  • ATransform to transformxy to screen coordinates.

  • A function with one of the following signatures:

    deftransform(renderer)->Bboxdeftransform(renderer)->Transform

    whererenderer is aRendererBase subclass.

    The result of the function is interpreted like theArtist andTransform cases above.

  • A tuple(xcoords, ycoords) specifying separate coordinatesystems forx andy.xcoords andycoords must each beof one of the above described types.

SeeAdvanced annotation for more details.

textcoordssingle or two-tuple of str orArtist orTransform or callable, default: value ofxycoords

The coordinate system thatxytext is given in.

Allxycoords values are valid as well as the following strings:

Value

Description

'offset points'

Offset, in points, from thexy value

'offset pixels'

Offset, in pixels, from thexy value

'offset fontsize'

Offset, relative to fontsize, from thexy value

arrowpropsdict, optional

The properties used to draw aFancyArrowPatch arrow between thepositionsxy andxytext. Defaults to None, i.e. no arrow isdrawn.

For historical reasons there are two different ways to specifyarrows, "simple" and "fancy":

Simple arrow:

Ifarrowprops does not contain the key 'arrowstyle' theallowed keys are:

Key

Description

width

The width of the arrow in points

headwidth

The width of the base of the arrow head in points

headlength

The length of the arrow head in points

shrink

Fraction of total length to shrink from both ends

?

AnyFancyArrowPatch property

The arrow is attached to the edge of the text box, the exactposition (corners or centers) depending on where it's pointing to.

Fancy arrow:

This is used if 'arrowstyle' is provided in thearrowprops.

Valid keys are the followingFancyArrowPatch parameters:

Key

Description

arrowstyle

The arrow style

connectionstyle

The connection style

relpos

See below; default is (0.5, 0.5)

patchA

Default is bounding box of the text

patchB

Default is None

shrinkA

In points. Default is 2 points

shrinkB

In points. Default is 2 points

mutation_scale

Default is text size (in points)

mutation_aspect

Default is 1

?

AnyFancyArrowPatch property

The exact starting point position of the arrow is defined byrelpos. It's a tuple of relative coordinates of the text box,where (0, 0) is the lower left corner and (1, 1) is the upperright corner. Values <0 and >1 are supported and specify pointsoutside the text box. By default (0.5, 0.5), so the starting pointis centered in the text box.

annotation_clipbool or None, default: None

Whether to clip (i.e. not draw) the annotation when the annotationpointxy is outside the Axes area.

  • IfTrue, the annotation will be clipped whenxy is outsidethe Axes.

  • IfFalse, the annotation will always be drawn.

  • IfNone, the annotation will be clipped whenxy is outsidethe Axes andxycoords is 'data'.

**kwargs

Additional kwargs are passed toText.

Returns:
Annotation

See also

Annotations

Examples usingmatplotlib.axes.Axes.annotate#

Simple axis pad

Simple axis pad

Hat graph

Hat graph

Timeline with lines, dates, and text

Timeline with lines, dates, and text

Patheffect Demo

Patheffect Demo

A pie and a donut with labels

A pie and a donut with labels

Mmh Donuts!!!

Mmh Donuts!!!

XKCD

XKCD

Ishikawa Diagram

Ishikawa Diagram

Combine two subplots using subplots and GridSpec

Combine two subplots using subplots and GridSpec

Scale invariant angle label

Scale invariant angle label

Annotate transform

Annotate transform

Annotating a plot

Annotating a plot

Annotate plots

Annotate plots

Annotate polar plots

Annotate polar plots

Annotation arrow style reference

Annotation arrow style reference

Labelling subplots

Labelling subplots

Concatenate text objects with different properties

Concatenate text objects with different properties

Render math equations using TeX

Render math equations using TeX

Text properties

Text properties

Annotation with units

Annotation with units

Faster rendering by using blitting

Faster rendering by using blitting

Transformations Tutorial

Transformations Tutorial

Quick start guide

Quick start guide

Annotations

Annotations

Text in Matplotlib

Text in Matplotlib