📜  Python中的 Matplotlib.pyplot.annotate()

📅  最后修改于: 2022-05-13 01:55:45.143000             🧑  作者: Mango

Python中的 Matplotlib.pyplot.annotate()

Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 PyplotMatplotlib模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。

matplotlib.pyplot.annotate()函数

matplotlib 库的 pyplot 模块中的annotate()函数用于用文本 s 对点 xy 进行注释。

下面的示例说明了 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()

输出: