2014-12-23 41 views
2

我该如何绘制一条曲线(可能是3d),以显示它正在发生的方向。例如,要显示圆形平面曲线顺时针或逆时针旋转。等高线/带有方向的曲线

喜欢这里的一个曲线, http://mathworld.wolfram.com/CauchyIntegralFormula.html

我不知道,即使有类似的功能,现在,所以我没有一个例子来告诉你。 感谢您的阅读。

编辑:我在这方面搜索了很多,不要以为你也可以在gnuplot上做到这一点。

+0

可能重复[箭头Matplotlib曲线蜱(HTTP ://stackoverflow.com/questions/26911898/matplotlib-curve-with-arrow-ticks) – Schorsch

+0

另一个需要对此应用程序进行一些更改。这一般适用于参数曲线。感谢您的关注。 – user3856487

回答

1

有趣的问题。我没有时间超过一个快速和肮脏的黑客多,所以在这里我们去(从优从代码中MPL启发streamplot

import matplotlib.lines as mlines 
import matplotlib.patches as mpatches 
import matplotlib.pyplot as plt 
import numpy as np 

def add_arrow_to_line2D(
    axes, line, arrow_locs=[0.2, 0.4, 0.6, 0.8], 
    arrowstyle='-|>', arrowsize=1, transform=None): 
    """ 
    Add arrows to a matplotlib.lines.Line2D at selected locations. 

    Parameters: 
    ----------- 
    axes: 
    line: list of 1 Line2D obbject as returned by plot command 
    arrow_locs: list of locations where to insert arrows, % of total length 
    arrowstyle: style of the arrow 
    arrowsize: size of the arrow 
    transform: a matplotlib transform instance, default to data coordinates 

    Returns: 
    -------- 
    arrows: list of arrows 
    """ 
    if (not(isinstance(line, list)) or not(isinstance(line[0], 
              mlines.Line2D))): 
     raise ValueError("expected a matplotlib.lines.Line2D object") 
    x, y = line[0].get_xdata(), line[0].get_ydata() 

    arrow_kw = dict(arrowstyle=arrowstyle, mutation_scale=10 * arrowsize) 
    if transform is None: 
     transform = axes.transData 

    arrows = [] 
    for loc in arrow_locs: 
     s = np.cumsum(np.sqrt(np.diff(x) ** 2 + np.diff(y) ** 2)) 
     n = np.searchsorted(s, s[-1] * loc) 
     arrow_tail = (x[n], y[n]) 
     arrow_head = (np.mean(x[n:n + 2]), np.mean(y[n:n + 2])) 
     p = mpatches.FancyArrowPatch(
      arrow_tail, arrow_head, transform=transform, 
      **arrow_kw) 
     axes.add_patch(p) 
     arrows.append(p) 
    return arrows 


fig, ax = plt.subplots(1, 1) 
t = np.linspace(0., 4*np.pi, 100.) 
line = ax.plot(np.log(t+1)*np.cos(t), np.log(t+1)*np.sin(t),"-") 
add_arrow_to_line2D(ax, line, arrow_locs=[0.1, 0.2, 0.3, 0.4, 0.6, 0.8, 0.99], 
        arrowsize=1.5) 

ax.axis("equal") 
ax.set_xlim([-4., 4.]) 
ax.set_ylim([-4., 4.]) 
plt.show() 

enter image description here

+0

感谢您的代码,它的作品,效果也不错。 – user3856487