简单的双头箭头
我想制作一个简单的箭头和一个双头箭头。我用下面的方法做了一个简单的箭头,但我觉得这可能不是最简单的方式:
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()
我找不到用这种方法制作双头箭头的方法。
4 个回答
2
你可以在同一条线上画出两个单头箭头,方向是相反的。
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()
6
你可以使用 matplotlib.patches.FancyArrowPatch
来画一个双头箭头。这个类可以让你设置 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)
这样就可以生成以下这些箭头:
16
你可以通过绘制两个重叠的 plt.arrow
来创建双头箭头。下面的代码可以帮助你实现这个效果。
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()
结果如下所示:
你可以看到,红色箭头是先绘制的,然后是绿色箭头。当你提供正确的坐标时,它就看起来像一个双头箭头。
56
你可以使用 annotate
方法来创建一个双头箭头。方法里可以不写任何文字注释,同时在 arrowprops
字典中设置 arrowstyle='<->'
,就像下面的例子所示:
import matplotlib.pyplot as plt
plt.annotate(s='', xy=(1,1), xytext=(0,0), arrowprops=dict(arrowstyle='<->'))
plt.show()