2017-04-25 229 views
3

我想要一条曲线旁边的箭头。例如:如何在matplotlib中的曲线末端放置箭头?

import numpy as np 
import matplotlib.pyplot as plt 

X = np.linspace(0,4*np.pi,10000) 
Y = np.sin(X) 

shift = 0.1 
seg_size = 300 

i = 0 
plt.plot(X,Y,color='blue') 
while i +seg_size < len(X): 
    x = X[i:i+seg_size] 
    y = Y[i:i+seg_size]+shift 
    plt.plot(x,y,color='black') 
    #input here command for arrow head 
    i += seg_size*2 

plt.show() 

我试图计算下一行的角度在曲线的末端,绘制箭头线,但我做错了什么和箭头头部变形。任何提示?

回答

0

如何使用

numpy.annotate() 

功能?

见这里的pyplot.annotate()函数创建箭头的例子:

https://matplotlib.org/examples/pylab_examples/annotation_demo.html

+0

''numpy''或''pyplot''?你给出的链接是后者,它不允许将曲线变成箭头,只能是直线。我可以“在曲线末尾作弊并放置注释 – Yotam

+0

如果我们取曲线的终点并将其用作pyplot注释的起点(箭头),它会起作用吗? –

+1

是的,我做了类似的事情该点不能太远,因为曲线并不总是接近于线性的 – Yotam

1

的FancyArrowPatch类需要一个路径作为参数,所以我想你可以使用它。

1)对于每个线段,创建一个matplotlib.path.Path实例。

2)使用路径实例绘制箭头。

import numpy as np 
import matplotlib.pyplot as plt; plt.ion() 
from matplotlib.patches import FancyArrowPatch, PathPatch 
from matplotlib.path import Path 

def create_path(x,y): 
    vertices = zip(x,y) 
    codes = [Path.MOVETO] + (len(vertices)-1) * [Path.CURVE3] 
    return Path(vertices, codes) 

X = np.linspace(0,4*np.pi,10000) 
Y = np.sin(X) 

fig, ax = plt.subplots(1,1) 
ax.plot(X,Y,color='blue') 

shift = 0.1 
seg_size = 300 

i = 0 
while i +seg_size < len(X): 
    x = X[i:i+seg_size] 
    y = Y[i:i+seg_size]+shift 

    path = create_path(x,y) 

    # for testing path 
    # patch = PathPatch(path, facecolor='none', lw=2) 
    # ax.add_patch(patch) 

    arrow = FancyArrowPatch(path=path, color='r') 
    ax.add_artist(arrow) 

    i += seg_size*2 

不幸的是,这并不工作,因为传递给FancyArrowPatch不能有超过2段的路径(没有记录,但有一个支票ensure_quadratic_bezier)。

所以你必须作弊。在下面我使用每段的最后2点绘制箭头。

import numpy as np 
import matplotlib.pyplot as plt; plt.ion() 
from matplotlib.patches import FancyArrowPatch 

X = np.linspace(0,4*np.pi,10000) 
Y = np.sin(X) 

fig, ax = plt.subplots(1,1) 
ax.plot(X,Y,color='blue') 

shift = 0.1 
seg_size = 300 

i = 0 
while i +seg_size < len(X): 
    x = X[i:i+seg_size] 
    y = Y[i:i+seg_size]+shift 

    ax.plot(x, y, 'k') 

    posA, posB = zip(x[-2:], y[-2:]) 
    edge_width = 2. 
    arrowstyle = "fancy,head_length={},head_width={},tail_width={}".format(2*edge_width, 3*edge_width, edge_width) 
    arrow = FancyArrowPatch(posA=posA, posB=posB, arrowstyle=arrowstyle, color='k') 
    ax.add_artist(arrow) 

    i += seg_size*2 

enter image description here