Matplotlib.axes.Axes.annotate() in Python - GeeksforGeeks

13 Apr, 2020
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
The Axes.annotate() function in axes module of matplotlib library is also used to annotate the point xy with text text.In other word, it i used to placed the text at xy.
Syntax:
Axes.annotate(self, s, xy, *args, **kwargs)
Parameters: This method accept the following parameters that are described below:
s: This parameter is the text of the annotation.
xy: This parameter is the point (x, y) to annotate.
xytext: This parameter is an optional parameter. It is The position (x, y) to place the text at.
xycoords: This parameter is also an optional parameter and contains the string value.
textcoords: This parameter contains the string value.Coordinate system that xytext is given, which may be different than the coordinate system used for xy
arrowprops : This parameter is also an optional parameter and contains dict type.Its default value is None.
annotation_clip : This parameter is also an optional parameter and contains boolean value.Its default value is None which behaves as True.
Returns: This method returns the annotation.
Below examples illustrate the matplotlib.axes.Axes.annotate() function in matplotlib.axes:
Example-1:
# Implementation of matplotlib functionimport matplotlib.pyplot as pltimport numpy as np   fig, ax1 = plt.subplots()   t = np.arange(4, 50., 1)s = np.cos(np.pi * t)**3- np.sin(3 * np.pi * t)**2  ax1.plot(t, s, lw = 2)ax1.annotate('Starting', xy =(3.3, 1),             xytext =(3, 1.8),             arrowprops = dict(facecolor ='green',                               shrink = 0.05),   )  ax1.set_ylim(-2, 2)ax1.set_title('matplotlib.axes.Axes.annotate() Example')plt.show()
Output:
Example-2:
# Implementation of matplotlib functionimport numpy as npimport matplotlib.pyplot as plt   x = np.arange(0, 20, 0.005)y = 3.5 * np.exp(-x / 3.) * np.sin(2 * np.pi * x)   fig, ax = plt.subplots()ax.plot(x, y, color ="green")ax.set_xlim(0, 20)ax.set_ylim(-4, 4)   xdata, ydata = 5, 0xdisplay, ydisplay = ax.transData.transform((xdata,                                              ydata))   bbox = dict(boxstyle ="round", fc ="0.8")arrowprops = dict(    arrowstyle = "->",    connectionstyle = "angle, angleA = 0, \    angleB = 90, rad = 10")   offset = 72   # Annotationax.annotate('data = (%.1f, %.1f)'%(xdata, ydata),            (xdata, ydata), xytext =(-2 * offset,                                     offset),             textcoords ='offset points',            bbox = bbox, arrowprops = arrowprops)      ax.annotate('display = (%.1f, %.1f)'%(xdisplay, ydisplay),            (xdisplay, ydisplay), xytext =(0.5 * offset,                                           -offset),            xycoords ='figure pixels',            textcoords ='offset points',            bbox = bbox, arrowprops = arrowprops)  ax.set_title('matplotlib.axes.Axes.annotate() Example')plt.show()
Output:
Python-matplotlib
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Reading and Writing to text files in Python
sum() function in Python