有没有办法用字符串列表注释散点图上的每个点?

Is there a way to annotate each point on a scatter with a list of strings?

我使用 variable=plt.scatter(test1,test2) 绘制了散点图,其中 test1test2 是对应于 x 和 y 的列表。

有没有办法用我创建的字符串列表或可变颜色来注释每个点?

我发现:

for i, txt in enumerate(variablelabel):
    variable.annotate(txt, (test1[i],test2[i]))

其中 variablelabel 定义为我的字符串列表。不幸的是,这似乎没有注释我的散点图。

或者,我发现您可以使用以下类似代码添加箭头:

ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
arrowprops=dict(facecolor='black', shrink=0.05),
ax.set_ylim(-2,2)
plt.show()

但这会产生我不想要的大箭头。我只想要列表中的字符串。

对不起,如果我不是很清楚。

您可以用字符串列表来注释每个点。使用 matplotlib.annotate is the solution. However you call annotate on a matplotlib.collections.PathCollection object (result of matplotlib.scatter) instead of a matplotlib.axes.Axes 对象。

在您的代码中:

variable = plt.scatter(test1, test2)
for i, txt in enumerate(variablelabel):
    variable.annotate(txt, (test1[i], test2[i]))

variable 是一个 matplotlib.collections.PathCollection。而是使用以下内容:

plt.scatter(test1, test2)
for i, txt in enumerate(variablelabel):
    plt.annotate(txt, (test1[i], test2[i]))

你应该得到这样的东西:

希望对您有所帮助。