Matplotlib:如何在路径补丁中添加箭头?

时间:2018-04-30 03:16:01

标签: python matplotlib

我有一些路径补丁。

pp1 = mpatches.PathPatch(Path([(start_x, height), (middle_x, middle_y), (end_x, height)],[Path.MOVETO, Path.CURVE3, Path.CURVE3]),fc="none", transform=ax1.transData)
ax1.add_patch(pp1)

这给了我弯曲的边缘,如下图所示: enter image description here

如何在这些弯曲边缘的末端/开始/中间添加箭头?

我做了一些搜索,并想出了其他包含箭头的补丁。我尝试了它们,但是将它们与我的路径补丁对齐并证明是非平凡的。我想知道是否有一种更容易的方法来向我的边缘添加箭头。

1 个答案:

答案 0 :(得分:1)

我认为在任意曲线中间添加箭头可能很简单,如this answer所示。

但是,您可以使用patches.FancyArrowPatch在曲线的两端/两端都有箭头:

import matplotlib.path as mpath
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

Path = mpath.Path

fig, ax = plt.subplots()
fap1 = mpatches.FancyArrowPatch(path=Path([(0, 0), (1, 1), (1, 0)],
                                         [Path.MOVETO, Path.CURVE3, Path.CURVE3]),
                                arrowstyle="<|-|>,head_length=10,head_width=10")
ax.add_patch(fap1)
ax.set_xlim(-1, 2)
ax.set_ylim(-1, 2)

plt.show()

enter image description here