matplotlib mpatches.FancyArrowPatch太短 [英] matplotlib mpatches.FancyArrowPatch is too short

查看:48
本文介绍了matplotlib mpatches.FancyArrowPatch太短的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 mpatches.FancyArrowPatch 来绘制很多路径(单个图中有数百条).我曾经使用过 plt.arrow ,但是它会使绘图窗口变慢,并且比补丁方法花费的时间更长.

无论如何,当我开始使用 mpatches.Arrow 时,我在大尺度上获得了不错的结果,但缩小了箭头尺寸

如果您需要一个集合来添加箭头,将 shrinkA shrinkB 参数设置为0可能就足够了.

 将matplotlib.pyplot导入为plt导入 matplotlib.patches 作为 mpatches从matplotlib.collections导入PatchCollection箭头 = mpatches.FancyArrowPatch((1,1), (3,3),shrinkA=0,shrinkB=0)collection = PatchCollection([箭头])plt.gca().add_collection(collection)plt.plot([1,3],[1,3],"rx",markersize = 15)plt.xlim(0,6)plt.ylim(0, 6)plt.show()

I want to use mpatches.FancyArrowPatch to plot a lot of paths (hundreds in a single plot). I used to use plt.arrow, but it makes the plot windows way slow and also takes longer than the patches approach.

Anyway, When I started using mpatches.Arrow I got good results for large scales, but scaling down the arrow sizes leads to a weird bug were the tail becomes triangular.. That's why I am using FancyArrowPatch now, which scales very well thanks to the dpi_corr kwarg.

But now take a look at the picture: The bottom arrow is a mpatches.Arrow and the top one is a mpatches.FancyArrowPatch and the red crosses mark start and end of the arrows. The top one is way too short! What's happening here? How can I make it the correct size?

Additional info: In my main program I have large lists containing start and end coordinates. From those I create arrows in a for loop using the function(s) you see below. I am using python 3.4 and matplotlib 2.0.2.

Here is my 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()

解决方案

If you just add the arrow as a patch, everything works as expected.

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()

In case you need a collection to add the arrow to, it might be enough to set the shrinkA and shrinkB arguments to 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()

这篇关于matplotlib mpatches.FancyArrowPatch太短的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆