matplotlib mpatches.FancyArrowPatch太短了

时间:2017-06-21 11:06:12

标签: python matplotlib plot

我想使用mpatches.FancyArrowPatch绘制很多路径(单个绘图中有数百个)。我过去常常使用plt.arrow,但它会使绘图窗口变慢,并且需要比补丁方法更长的时间。

无论如何,当我开始使用mpatches.Arrow时,我获得了很好的大比例结果,但缩小了箭头尺寸leads to a weird bug were the tail becomes triangular.。这就是我现在使用FancyArrowPatch的原因,这要归功于dpi_corr kwarg。

但是现在看看图片:底部箭头是mpatches.Arrow,顶部箭头是mpatches.FancyArrowPatch,红色标记箭头的开始和结束标记。最上面的一个方式太短了!这里发生了什么?如何使其尺寸正确?

附加信息:在我的主程序中,我有包含开始和结束坐标的大型列表。从那些我使用你在下面看到的函数在for循环中创建箭头。我使用的是python 3.4matplotlib 2.0.2

fancyarrowpatch plot

这是我的MWE:

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection

start1 = [1, 1]
end1 = [3, 3]
start2 = [1, 3]
end2 = [3, 5]

def patchArrow(A, B):
    arrow = mpatches.Arrow(A[0], A[1], B[0] - A[0], B[1] - A[1])
    return arrow

def patchFancyArrow(A, B):
    arrow = mpatches.FancyArrowPatch((A[0], A[1]), (B[0], B[1]))
    return arrow

patches = []
patches.append(patchArrow(start1, end1))
patches.append(patchFancyArrow(start2, end2))
collection = PatchCollection(patches)
plt.plot([1, 3, 1, 3], [1, 3, 3, 5], "rx", markersize=15)
plt.gca().add_collection(collection)
plt.xlim(0, 6)
plt.ylim(0, 6)
plt.show()

1 个答案:

答案 0 :(得分:1)

如果你只是将箭头添加为补丁,一切都按预期工作。

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

style="Simple,head_length=28,head_width=36,tail_width=20"
arrow = arrow = mpatches.FancyArrowPatch((1,1), (3,3), arrowstyle=style)
plt.gca().add_patch(arrow)

plt.plot([1, 3], [1,3], "rx", markersize=15)
plt.xlim(0, 6)
plt.ylim(0, 6)
plt.show()

enter image description here

如果您需要一个集合来添加箭头,那么将shrinkAshrinkB参数设置为0就足够了。

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection


arrow = mpatches.FancyArrowPatch((1,1), (3,3),  shrinkA=0,  shrinkB=0)
collection = PatchCollection([arrow])
plt.gca().add_collection(collection)

plt.plot([1, 3], [1,3], "rx", markersize=15)
plt.xlim(0, 6)
plt.ylim(0, 6)
plt.show()

enter image description here