【问题标题】:How do you offset text in a scatter plot in matplotlib?如何在 matplotlib 的散点图中偏移文本?
【发布时间】:2020-03-21 09:10:15
【问题描述】:

我在我的 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'))

【问题讨论】:

标签: python data-visualization matplotlib


【解决方案1】:

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

annotation tutorial 上的更多示例

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')
            )

【讨论】:

  • 不客气。如果您的问题得到解决,请考虑使用左侧的复选标记接受答案以关闭主题
  • 当我尝试将“偏移点”与文本注释一起使用时,出现此错误:ValueError: xycoords cannot be an offset coordinate
猜你喜欢
  • 2016-07-17
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-14
  • 2020-07-22
相关资源
最近更新 更多