首页 > 解决方案 > 如何使用 FancyArrowPatch 制作不同类型的箭头?

问题描述

FancyArrowPatch我想在 matplotlib 中使用以下箭头:

在此处输入图像描述

产生上述情节的代码是(取自这里):

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(4,4))

v = [-0.2, 0, .2, .4, .6, .8, 1]
for i, overhang in enumerate(v):
    ax.arrow(.1,overhang,.6,0, width=0.001, color="k", 
             head_width=0.1, head_length=0.15, overhang=overhang)

ax.set_yticks(v)
ax.set_xticks([])
ax.set_ylabel("overhang")
ax.set_ylim(-0.3,1.1)
plt.tight_layout()
plt.show()

上面的代码使用ax.arrow. 我怎样才能实现这个FancyArrowPatch

标签: pythonmatplotlib

解决方案


也许它有助于试验FancyArrowPatch, ArrowStyle

from matplotlib.patches import FancyArrowPatch, ArrowStyle
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
style = ArrowStyle('Fancy', head_length=1, head_width=1.5, tail_width=0.5)
arrow = FancyArrowPatch((0, 0), (1, 1), mutation_scale=25, arrowstyle=style, color='k')                     
ax.add_patch(arrow)

plt.xlim(-0.1, 1.1)
plt.ylim(-0.1, 1.1)

现在可以根据需要调整各种参数以获得所需的结果(例如head_width,,,,head_length... mutation_scale

此代码段使用以下代码重现给定的情节matplotlib.patches.FancyArrow

from matplotlib.patches import FancyArrow
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
v = [-0.2, 0, .2, .4, .6, .8, 1]
for i, overhang in enumerate(v):
    arrow = FancyArrow(0, overhang, 1, 0, width = 0.001, head_width=0.1, head_length = None, color = 'k', overhang = overhang)
    ax.add_patch(arrow)

ax.set_xticks([])
ax.set_yticks(v)
ax.set_ylim(-0.3,1.1)
ax.set_xlim(-0.5, 2)
ax.set_ylabel("overhang")

推荐阅读