使用注释在 matplotlib 中创建箭头

Create arrows in matplotlib using annotate

我正在用

注释我的情节
plt.annotate(
    '30.2',
    xy=(3, y),
    xycoords='data',
    xytext=(3, y - 5),
    textcoords='data',
    arrowprops=dict(facecolor='black', arrowstyle="->")
)

它按预期创建了向下箭头,但它们并不完全垂直。

如果我将 '30.2' 更改为空字符串 '',它们是垂直的。

无论文本字符串有多长,如何确保箭头是垂直的?

这只是文本对齐的问题。试试这个:

plt.annotate(
    '30.2',
    xy=(3, y),
    xycoords='data',
    xytext=(3, y - 5),
    textcoords='data',
    horizontalalignment='center',
    arrowprops=dict(facecolor='black', arrowstyle="->")
)