python - 如何在 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'))
解决方案
用于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')
)
推荐阅读
- python - 熊猫重采样不会改变时间增量
- android - 如何为 Widget.MaterialComponents.Button 添加边距?
- node.js - Cloudfront 不使用 lambda 函数
- c++ - C ++ 17调用没有尖括号的模板类的静态方法
- google-cloud-platform - 有没有办法修改谷歌云任务设置?
- widget - 如何以编程方式在我的 Android 上完全禁用小部件?
- heroku - 提交从 Heroku 到 Github / BitBucket 的更改
- php - 在 nginx 中,如何从 localhost 中获取从 localhost 发送的响应?
- vb.net - Vb.net 表单在一段时间后冻结
- android - 使用初始 SQLite 数据库部署 Android 应用程序