Python中的 Matplotlib.pyplot.annotate()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Pyplot是Matplotlib模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。
matplotlib.pyplot.annotate()函数
matplotlib 库的 pyplot 模块中的annotate()函数用于用文本 s 对点 xy 进行注释。
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.
下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.annotate()函数:
示例 #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()
输出:
示例 #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()
输出: