Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface.
matplotlib.pyplot.annotate() Function
The annotate() function in pyplot module of matplotlib library is used to annotate the point xy with text s.
Syntax: angle_spectrum(x, Fs=2, Fc=0, window=mlab.window_hanning, pad_to=None, sides=’default’, **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.pyplot.annotate() function in matplotlib.pyplot:
Example #1:
# Implementation of matplotlib.pyplot.annotate() # function import matplotlib.pyplot as plt import numpy as np fig, geeeks = plt.subplots() t = np.arange( 0.0 , 5.0 , 0.001 ) s = np.cos( 3 * np.pi * t) line = geeeks.plot(t, s, lw = 2 ) # Annotation geeeks.annotate( 'Local Max' , xy = ( 3.3 , 1 ), xytext = ( 3 , 1.8 ), arrowprops = dict (facecolor = 'green' , shrink = 0.05 ),) geeeks.set_ylim( - 2 , 2 ) # Plot the Annotation in the graph plt.show() |
Output:
Example #2:
# Implementation of matplotlib.pyplot.annotate() # function import numpy as np import matplotlib.pyplot as plt x = np.arange( 0 , 10 , 0.005 ) y = np.exp( - x / 3. ) * np.sin( 3 * np.pi * x) fig, ax = plt.subplots() ax.plot(x, y) ax.set_xlim( 0 , 10 ) ax.set_ylim( - 1 , 1 ) # Setting up the parameters 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) disp = ax.annotate( 'display = (%.1f, %.1f)' % (xdisplay, ydisplay), (xdisplay, ydisplay), xytext = ( 0.5 * offset, - offset), xycoords = 'figure pixels' , textcoords = 'offset points' , bbox = bbox, arrowprops = arrowprops) # To display the annotation plt.show() |
Output: