Joel
Joel

Reputation: 23827

How to make an arrowhead point in the same direction as the arrow using 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?)

Upvotes: 3

Views: 1590

Answers (1)

Y. Luo
Y. Luo

Reputation: 5722

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

Upvotes: 3

Related Questions