Skip to content Skip to sidebar Skip to footer

Python Annotation Arrows With Same Headlength

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,

Solution 1:

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.

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()

Result:

enter image description here

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.

Post a Comment for "Python Annotation Arrows With Same Headlength"