# Matplotlib 标记散点

Suraj Joshi 2023年1月30日

## 使用 `matplotlib.pyplot.annotate()` 函数为散点图点添加标签

``````matplotlib.pyplot.annotate(text, xy, *args, **kwargs)
``````

``````import numpy as np
import matplotlib.pyplot as plt

np.random.seed(20)

X = np.random.randint(10, size=(5))
Y = np.random.randint(10, size=(5))

annotations = ["Point-1", "Point-2", "Point-3", "Point-4", "Point-5"]

plt.figure(figsize=(8, 6))
plt.scatter(X, Y, s=100, color="red")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Scatter Plot with annotations", fontsize=15)
for i, label in enumerate(annotations):
plt.annotate(label, (X[i], Y[i]))

plt.show()
``````

## 使用 `matplotlib.pyplot.text()` 函数为散点图点添加标签

``````matplotlib.pyplot.text(x, y, s, fontdict=None, **kwargs)
``````

``````import numpy as np
import matplotlib.pyplot as plt

np.random.seed(20)

X = np.random.randint(10, size=(5))
Y = np.random.randint(10, size=(5))

annotations = ["Point-1", "Point-2", "Point-3", "Point-4", "Point-5"]

plt.figure(figsize=(8, 6))
plt.scatter(X, Y, s=100, color="red")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Scatter Plot with annotations", fontsize=15)
for i, label in enumerate(annotations):
plt.text(X[i], Y[i], label)

plt.show()
``````

Suraj Joshi is a backend software engineer at Matrice.ai.