What's the difference between Axes.text() and Axes.annotate() in Python matplotlib

Viewed 2388

The document says

Axes.text(self, x, y, s, fontdict=None, withdash=deprecated parameter, **kwargs)

Add text to the axes.

Add the text s to the axes at location x, y in data coordinates.

Axes.annotate(self, s, xy, *args, **kwargs)

Annotate the point xy with text s.

In the simplest form, the text is placed at xy.

Optionally, the text can be displayed in another position xytext. An arrow pointing from the text to the annotated point xy can then be added by defining arrowprops.

Both Axes.text() and Axes.annotate() can add text to location x, y. The coordinate system can be changed using the transform parameter in Axes.text() while it is the xycoords parameter in Axes.annotate(). However, Axes.annotate() can draw an arrow using the arrowprops parameter while Axes.text() cann't. Another difference I can see is the return value.

So I think Axes.annotate() is a superset of Axes.text(). Does it mean Axes.text() is useless? When should I use Axes.text() rather than Axes.annotate()?

1 Answers

From the source code documentation:

class Annotation(Text, _AnnotationBase):
    """
    An `.Annotation` is a `.Text` that can refer to a specific position *xy*.
    Optionally an arrow pointing from the text to *xy* can be drawn.

Also, the Annotation subclasses Text. Here, Text is more general function to display text and annotate is the function display text associated to a point (or a location) in the plot. (especially, with the arrow-pointing support)

Related