Python的注释与箭一样headlength [英] Python annotation arrows with same headlength
问题描述
我使用matplotlib来创建箭头标注。
I'm using matplotlib to create annotations with arrows.
from matplotlib import pyplot as plt
plt.figure()
plt.annotate('Text1', xy=(1, 0), xytext=(1, 1), arrowprops=dict(alpha=0.5, fc='r', ec='r', headwidth=10))
plt.annotate('Text2', xy=(3, 0), xytext=(3, 3), arrowprops=dict(alpha=0.5, fc='r', ec='r', headwidth=10))
plt.annotate('Text3', xy=(6, 0), xytext=(6, 6), arrowprops=dict(alpha=0.5, fc='r', ec='r', headwidth=10))
plt.annotate('Text4', xy=(9, 0), xytext=(9, 9), arrowprops=dict(alpha=0.5, fc='r', ec='r', headwidth=10))
plt.xlim((0, 10))
plt.ylim(0, 10)
plt.show()
可以看出,箭头头的长度由行长度的影响,并且箭头之间变化。
As can be seen, the length of the arrow heads is affected by the line-length and varies between the arrows.
我在寻找一种方法来创建不同的线路长度,但平等箭箭headlengths
尽管很多选项都注释手册上列出的,我是不是能够产生期望的结果。
I'm looking for a way to create arrows with different line-lengths but equal arrow-headlengths. Although many options are listed on the annotations manual page, i was not able to produce the desired result.
作为一个替代方法,我试图用seperatedely的 plt.arrow 的绘制箭头。在这种情况下,箭头头部看上去根据需要,但使用透明度时,附加的线变得可见。
As an alternative approach i tried to plot the arrows seperatedely using plt.arrow. In that case the arrow heads looked as desired, but when using transparency, additional lines became visible.
最好的问候,zinjaai
Best regards, zinjaai
推荐答案
在 arrowprops
您不能指定头的长度,但压裂
实际参数的指定为分数的箭头长度头部的长度。既然你知道 XY
和 xytext
就可以计算所需的分数为所需headlength。
In the arrowprops
you can't specify the length of the head, but the frac
arguement specifies the length of the head as a fraction the arrow length. Since you know xy
and xytext
you can compute the necessary fraction for a desired headlength.
我为您提供的例子快快行动吧(带的头长 0.5
)。
I've don't it quickly for the example you provided (with an head length of 0.5
).
from matplotlib import pyplot as plt
plt.figure()
plt.annotate('Text1', xy=(1, 0), xytext=(1, 1),
arrowprops=dict(alpha=0.5, fc='r', ec='r', headwidth=10, frac=0.5/1))
plt.annotate('Text2', xy=(3, 0), xytext=(3, 3),
arrowprops=dict(alpha=0.5, fc='r', ec='r', headwidth=10, frac=0.5/3))
plt.annotate('Text3', xy=(6, 0), xytext=(6, 6),
arrowprops=dict(alpha=0.5, fc='r', ec='r', headwidth=10, frac=0.5/6))
plt.annotate('Text4', xy=(9, 0), xytext=(9, 9),
arrowprops=dict(alpha=0.5, fc='r', ec='r', headwidth=10, frac=0.5/9))
plt.xlim((0, 10))
plt.ylim(0, 10)
plt.show()
结果:
您可以在一个功能,它计算箭头的长度包装这个(给 XY
和 xytext
) ,然后通过该上 plt.annotate
。
You could wrap this in a function, which computes the length of the arrow (given xy
and xytext
) and then passes that on to plt.annotate
.
这篇关于Python的注释与箭一样headlength的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!