Skip to main content
 首页 » 编程设计

python之在 matplotlib 中绘制带有中点箭头的圆形 fancyarrowpatch

2024年10月24日12wayfarer

我一直在尝试突破 matplotlib 补丁的界限,并指示它绘制一个圆形的 FancyArrowPatch,其中点上有一个方向箭头。这在我试图创建的网络表示中非常有用。

我使用 python 的编码时间还没有达到两位数,所以我不能说我对 matplotlib 的 patches.py 有清楚的了解,但我已经将解决​​方案缩小到两种可能的策略:

  1. 聪明的,可能是 Pythonic 的方式:创建一个自定义 arrowstyle 类,它进一步需要修改 _get_arrow_wedge() 函数以包含中点坐标。这可能超出了我现在的可能性,或者
  2. 惰性方法:从引出的 FancyArrowPatch 中提取中点坐标,并在此类坐标上绘制所需的 arrowstyle。

当然,到目前为止我选择了懒惰的方式。我做了一些早期实验,使用 get_path() 和 get_path_in_displaycoord() 提取弯曲的 FancyArrowPatch 的中点坐标,但我似乎无法预测精确的中点坐标。一些帮助将不胜感激。

到目前为止我的摆弄:

import matplotlib.pyplot as plt 
from matplotlib.patches import FancyArrowPatch 
 
n1 = (2,3) 
n2 = (4,6) 
 
 
# Try with multiple arc radius sizes, draw a separate plot each time 
for rad in range(20): 
    #setup figure 
    figure = plt.figure() 
    ax = plt.subplot(111) 
    plt.annotate('rad:' + str(rad/25.),xy=(2,5)) 
 
    # create rounded fancyarrowpatch    
    t = FancyArrowPatch(posA=n1,posB=n2, 
                        connectionstyle='arc3,rad=%s'%float(rad/25.), 
                        arrowstyle='->', 
                        shrinkA=0, 
                        shrinkB=0, 
                        mutation_scale=0.5) 
 
    # extract vertices from get_path: points P# 
    path = t.get_path().vertices.tolist() 
    lab, px, py = ['P{0}'.format(i) for i in range(len(path))], [u[0] for u in path],[u[1] for u in path] 
    for i in range(len(path)): 
        plt.annotate(lab[i],xy=(px[i],py[i])) 
 
    # extract vertices from get_path_in_displaycoord (but they are useless) : points G# 
    newpath = t.get_path_in_displaycoord() 
    a,b = newpath[0][0].vertices.tolist(), newpath[0][1].vertices.tolist() 
    a.extend(b) 
    glab, gx, gy = ['G{0}'.format(i) for i in range(len(a))], [u[0] for u in a],[u[1] for u in a] 
    for i in range(len(a)): 
        plt.annotate(glab[i],xy=(gx[i],gy[i]))     
 
    #point A: start 
    x1, y1 = n1 
    plt.annotate('A',xy=(x1,y1))     
 
    #point B:end 
    x2, y2 = n2 
    plt.annotate('B',xy=(x2,y2)) 
 
    #point M: the 'midpoint' as defined by class Arc3, specifically its connect() function 
    x12, y12 = (x1 + x2) / 2., (y1 + y2) / 2. 
    dx, dy = x2 - x1, y2 - y1 
    cx, cy = x12 + (rad/100.) * dy, y12 - (rad/100.) * dx     
    plt.annotate('M',xy=(cx,cy)) 
 
    #point O : midpoint between M and P1, the second vertex from get_path    
    mx,my = (cx + px[1])/2., (cy + py[1])/2. 
    plt.annotate('O',xy=(mx,my)) 
 
    ax.add_patch(t) 
    plt.scatter([x1,cx,x2,mx,gx].extend(px),[y1,cy,y2,my,gy].extend(py)) 
 
 
plt.show() 

编辑:采纳@cphlewis 的建议:我尝试重建贝塞尔曲线:

def bezcurv(start,control,end,tau): 
    ans = [] 
    for t in tau: 
        B = [(1-t)**2 * start[i] + 2*(1-t)*t*end[i] + (t**2)*control[i] for i in range(len(start))] 
        ans.append(tuple(B)) 
    return ans 

因此,我将生成的线添加到原始图中:

tau = [time/100. for time in range(101)] 
bezsim = bezcurv(n1,n2,(cx,cy),tau) 
simx,simy = [b[0] for b in bezsim], [b[1] for b in bezsim] 

下面的绿线是(应该是?)重建的贝塞尔曲线,但显然不是。

请您参考如下方法:

经过多次努力,我说服自己,要解决这个问题,我必须离开 FancyArrowPatch 套件并从头开始创建一些东西。这是一个可行的解决方案,它远未实现任何完美主义精神,但令我满意:

import matplotlib.pyplot as plt 
import numpy as np 
from numpy.random import seed, randint 
 
# Build function that connects two points with a curved line,  
# and an arrow on the middle of it 
 
seed(1679) 
 
narrow = 3 
rad_one = 50 
numpoints = 3 
 
random_points = list(randint(1,20,[numpoints,4])) 
rpoints = [[(a,b),(c,d)] for a,b,c,d in random_points] 
 
def curvline(start,end,rad,t=100,arrows=1,push=0.8): 
    #Compute midpoint 
    rad = rad/100.     
    x1, y1 = start 
    x2, y2 = end 
    y12 = (y1 + y2) / 2 
    dy = (y2 - y1) 
    cy = y12 + (rad) * dy 
    #Prepare line 
    tau = np.linspace(0,1,t) 
    xsupport = np.linspace(x1,x2,t) 
    ysupport = [(1-i)**2 * y1 + 2*(1-i)*i*cy + (i**2)*y2 for i in tau] 
    #Create arrow data     
    arset = list(np.linspace(0,1,arrows+2)) 
    c = zip([xsupport[int(t*a*push)] for a in arset[1:-1]], 
                      [ysupport[int(t*a*push)] for a in arset[1:-1]]) 
    dt = zip([xsupport[int(t*a*push)+1]-xsupport[int(t*a*push)] for a in arset[1:-1]], 
                      [ysupport[int(t*a*push)+1]-ysupport[int(t*a*push)] for a in arset[1:-1]]) 
    arrowpath = zip(c,dt) 
    return xsupport, ysupport, arrowpath 
 
def plotcurv(start,end,rad,t=100,arrows=1,arwidth=.25): 
    x, y, c = curvline(start,end,rad,t,arrows) 
    plt.plot(x,y,'k-') 
    for d,dt in c: 
        plt.arrow(d[0],d[1],dt[0],dt[1], shape='full', lw=0,  
                  length_includes_head=False, head_width=arwidth) 
    return c 
 
#Create figure 
figure = plt.figure() 
ax = plt.subplot(111) 
for n1,n2 in rpoints:     
    #First line 
    plotcurv(n1,n2,rad_one,200,narrow,0.5) 
    #Second line 
    plotcurv(n2,n1,rad_one,200,narrow,0.5) 
ax.set_xlim(0,20) 
ax.set_ylim(0,20) 
plt.show 

我用三个随机点测试了它,来回绘制线条。其中给出了下图:

该函数允许用户设置多个所需的箭头,并将它们均匀地放置在绘制的贝塞尔曲线上,确保表示正确的方向。然而,因为贝塞尔曲线并不完全是“弧”,我试探性地插入箭头的开始,使它们看起来更居中。对此解决方案的任何改进将不胜感激。