Matplotlib.axes.Axes.annotate() in Python
Last Updated :
13 Apr, 2020
Improve
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.
Python3
Output:
Example-2:
Python3
Output:
matplotlib.axes.Axes.annotate() Function
Syntax:Below examples illustrate the matplotlib.axes.Axes.annotate() function in matplotlib.axes: Example-1:Axes.annotate(self, s, xy, *args, **kwargs)Parameters: This method accept the following parameters that are described below:Returns: This method returns the annotation.
- 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.
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import 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()

# Implementation of matplotlib function
import numpy as np
import 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, 0
xdisplay, 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
# Annotation
ax.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()
