我不知道有任何 matplotlib 函数可以满足您的要求,但您可以使用 annotate 轻松注释您的观点:
import matplotlib.pyplot as plt
# So starting from your example
y = [65, 40, 180, 40]
x = [3, 3, 7, 5]
labels = ['low', 'low', 'high', 'undervalued']
fig, ax = plt.subplots()
ax.scatter(x, y)
for i, label in enumerate(labels):
ax.annotate(label, (x[i], y[i]))
或者,如果您对每个标签都有一些间隔,您可以编写一个返回所需标签的函数,例如:
import matplotlib.pyplot as plt
# Based on the image you provided
def get_label(x,y):
if x<4 and y<100:
return 'low'
elif x<4 and y>=100:
return 'overvalued'
elif x>=4 and y<100:
return 'undervalued'
else:
return 'high'
y = [65, 40, 180, 40]
x = [3, 3, 7, 5]
fig, ax = plt.subplots()
ax.scatter(x, y)
for x_current, y_current in zip(x,y):
ax.annotate(get_label(x_current, y_current), (x_current, y_current))
您可以在documentation 中找到更多格式。