Python的注释与箭一样headlength

编程入门 行业动态 更新时间:2024-10-25 22:32:41
本文介绍了Python的注释与箭一样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

本文发布于:2023-07-16 23:06:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1126877.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   Python   headlength

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!