Reputation: 2993
I would like to make a simple arrow and a two head arrow. I used the following to make a simple arrow, but I doubt this is the easiest method :
import matplotlib.pyplot as plt
arr_width = .009 # I don't know what unit it is here.
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(range(10))
ax1.arrow(1, 1, 0, .5, width = arr_width, head_width = 3 * arr_width,
head_length = 9 * arr_width)
plt.show()
I can't find how to make two head arrows with this method.
Upvotes: 36
Views: 75717
Reputation: 36249
You can use matplotlib.patches.FancyArrowPatch
to draw a two-headed arrow. This class allows to specify arrowstyle
:
import matplotlib.patches as patches
p1 = patches.FancyArrowPatch((0, 0), (1, 1), arrowstyle='<->', mutation_scale=20)
p2 = patches.FancyArrowPatch((1, 0), (0, 1), arrowstyle='<|-|>', mutation_scale=20)
This produces the following arrows:
Upvotes: 7
Reputation: 31
You can draw two one-headed arrows along the same line but in opposite directions.
import matplotlib.pyplot as plt
# Arrows
plt.arrow(0.3, 0.1, 0.4, 0.7, color='red', head_length = 0.07, head_width = 0.025, length_includes_head = True)
plt.arrow(0.7, 0.8, -0.4, -0.7, color='red', head_length = 0.07, head_width = 0.025, length_includes_head = True)
plt.show()
Upvotes: 3
Reputation: 449
You can create double-headed arrows by plotting two plt.arrow
which are overlapping. The code below helps to do that.
import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
# red arrow
plt.arrow(0.15, 0.5, 0.75, 0, head_width=0.05, head_length=0.03, linewidth=4, color='r', length_includes_head=True)
# green arrow
plt.arrow(0.85, 0.5, -0.70, 0, head_width=0.05, head_length=0.03, linewidth=4, color='g', length_includes_head=True)
plt.show()
And the result is like this:
You can see that the red arrow is being plotted firstly, and then the green one. When you supply the correct coordinates, it looks like a double-headed.
Upvotes: 16
Reputation: 53678
You can create a double-headed arrow using the annotate
method with blank text annotation and setting the arrowprops
dict to include arrowstyle='<->'
as shown below:
import matplotlib.pyplot as plt
plt.annotate(s='', xy=(1,1), xytext=(0,0), arrowprops=dict(arrowstyle='<->'))
plt.show()
Upvotes: 60