使用注释在matplotlib中创建箭头

时间:2022-12-05 22:58:12

I'm annotating my plot with

我正在诠释我的情节

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

It creates downward arrows as expected but they are not completely vertical.

它按预期产生向下箭头,但它们不是完全垂直的。

If I change '30.2' to a en empty string '', they are vertical.

如果我将'30 .2'改为空字符串'',它们是垂直的。

How can I make sure the arrows are vertical no matter how long the text string is?

无论文本字符串有多长,我怎样才能确保箭头是垂直的?

1 个解决方案

#1


It's just a matter of text alignment. Try this:

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

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

#1


It's just a matter of text alignment. Try this:

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

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