如何在 matplotlib 中偏移散点图中的文本?

ven*_*een 4 python data-visualization matplotlib

我在 PCA 中注释了两个特定点,但文本位于一堆点的中间,难以阅读。我想将其向下移动(并添加箭头,我认为我已经成功完成了)。有人可以帮忙吗?

我按照以下方式制作了文本:

for i, txt in enumerate(cluster_center_names):
    plt.annotate(txt,(x_cluster_center[i],y_cluster_center[i]), weight="bold", fontsize=10, arrowprops=dict(arrowstyle="->", color='black'))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Diz*_*ahi 8

用于xytext=(x,y)设置文本的坐标。您可以使用绝对值(数据、轴或图形坐标)或相对位置来提供这些坐标textcoords="offset points"

注释教程中的更多示例

x1,y1 = 0,0
x2,y2 = 20,50
fig, ax = plt.subplots()
ax.scatter(x1,y1)
ax.annotate("Annotation",
            xy=(x1, y1), xycoords='data',
            xytext=(x2, y2), textcoords='offset points',
            arrowprops=dict(arrowstyle="->", color='black')
            )
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述