我想使用 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.4
和 matplotlib 2.0.2
。
这是我的 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()
最佳答案
如果您只是将箭头添加为补丁,一切都会按预期工作。
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()
如果您需要一个集合来添加箭头,将 shrinkA
和 shrinkB
参数设置为 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()
关于python - mpatches.FancyArrowPatch 太短,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44674356/