Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an arrowhead point in the same direction as the arrow using matplotlib

Tags:

matplotlib

I'm using the code

import matplotlib.pyplot as plt

plt.figure(1)
plt.axis(xmin=0, xmax=60, ymin=0, ymax=0.25)
plt.arrow(30, 0.01, 10, 0.1, head_width=0.05, head_length=1, length_includes_head=True)
plt.savefig('arrow.png')

and I expect to get an arrowhead that is pointing in the same direction the line of the arrow points. But it doesn't:

enter image description here

What can be done about this (and why is it happening?)

like image 868
Joel Avatar asked Nov 17 '25 16:11

Joel


1 Answers

Part of the reason has been briefly mentioned here as:

Notes

The resulting arrow is affected by the axes aspect ratio and limits. This may produce an arrow whose head is not square with its stem. To create an arrow whose head is square with its stem, use annotate() for example:

Do you mind doing this?

import matplotlib.pyplot as plt

plt.figure(1)
plt.axis(xmin=0, xmax=60, ymin=0, ymax=0.25)
# plt.arrow(30, 0.01, 10, 0.1, head_width=0.05, head_length=1, length_includes_head=True)
plt.annotate("", xy=(40, 0.11), xytext=(30, 0.01), arrowprops=dict(headwidth=10, headlength=10, width=0.1))
plt.savefig('arrow.png')

enter image description here

like image 187
Y. Luo Avatar answered Nov 20 '25 18:11

Y. Luo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!