指向型注释文本
当需要向图像中的某个点或者某个区域添加注释内容时,需要用到annotate()函数。
代码展示
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
abs = np.random.randn(100)
y1 = 2 * x + 9 + abs
y2 = 2 * x + 9
plt.scatter(x, y1, c="red", label="scatter")
plt.scatter(2, 20, c="black", label="a single scatter")
plt.plot(x, y2, ls="-", lw=1, label="line")
plt.legend()
plt.xlabel("x")
plt.ylabel("y")
plt.annotate("a single scatter",
xy=(2, 20), xytext=(2, 23),
weight="bold", color="y",
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3",
color="y"))
plt.show()
重点代码展示
plt.annotate(string,
xy=(2, 20),
xytext=(2, 23),
weight="bold",
color="y",
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3",
color="y"))
- string:注释文本。
- xy:被注释的图形所在的位置。
- xytext:注释文本的位置。
- weight:注释文本字体的粗细。
- color:文本颜色。
- arrowprops:箭头风格的属性字典。