# Arrows on trajectory as it evolves

**URL:** https://discourse.julialang.org/t/arrows-on-trajectory-as-it-evolves/60733
**Category:** General Usage
**Tags:** plotting, graphics, plots
**Created:** [May 7, 2021, 5:05pm UTC](https://discourse.julialang.org/t/arrows-on-trajectory-as-it-evolves/60733 "2021-05-07T17:05:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Sahil\_Goyal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_goyal/32/7747_2.png) [@Sahil\_Goyal](https://discourse.julialang.org/u/Sahil_Goyal)
#### Post date: [May 7, 2021, 5:05pm UTC](https://discourse.julialang.org/t/arrows-on-trajectory-as-it-evolves/60733/1 "2021-05-07T17:05:46Z")

</div>

I have a curve as it evolves with time, I need to have arrow on the curve something like shown in the figure. It was plotted using Mathematica. Plot not necessarily need not be against time it can be plot of x vs y.  
 ![arrow_on_curve](https://global.discourse-cdn.com/julialang/original/3X/4/8/483d8fd10db1f0c694f9ec417f01764eb2920151.jpeg)

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 7, 2021, 5:22pm UTC](https://discourse.julialang.org/t/arrows-on-trajectory-as-it-evolves/60733/2 "2021-05-07T17:22:21Z")

</div>

You can do this with Makie. See this thread:

> [@Is there a streamlines function/package?](https://discourse.julialang.org/t/is-there-a-streamlines-function-package/34800):
>
> Does anyone know of a package to compute 3D streamlines in Julia? Like Matlab’s: [Compute 3-D streamline data - MATLAB stream3 - MathWorks Deutschland](https://de.mathworks.com/help/matlab/ref/stream3.html) I specifically want to get the XYZ data describing the streamlines. If not a package in Python that I can retrieve these with PyCall would also work. Tim

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 7, 2021, 5:23pm UTC](https://discourse.julialang.org/t/arrows-on-trajectory-as-it-evolves/60733/3 "2021-05-07T17:23:11Z")

</div>

With Plots.jl you can plot arrows with

`plot(xs, ys, arrow=true)`

So you could split up your input data into separate lines (e.g. using NaNs?) and plot arrows like this.

---

<div class="post-metadata">

### Author: ![Sahil\_Goyal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_goyal/32/7747_2.png) [@Sahil\_Goyal](https://discourse.julialang.org/u/Sahil_Goyal)
#### Post date: [May 7, 2021, 7:17pm UTC](https://discourse.julialang.org/t/arrows-on-trajectory-as-it-evolves/60733/4 "2021-05-07T19:17:41Z")

</div>

I know about streamplot. It can give the desired answer but I don’t need all the trajectories, I need just specific trajectories.

---

<div class="post-metadata">

### Author: ![Sahil\_Goyal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_goyal/32/7747_2.png) [@Sahil\_Goyal](https://discourse.julialang.org/u/Sahil_Goyal)
#### Post date: [May 7, 2021, 7:20pm UTC](https://discourse.julialang.org/t/arrows-on-trajectory-as-it-evolves/60733/5 "2021-05-07T19:20:26Z")

</div>

Points are the solution of the differential equation. There are some issues with it.  
Issue-1 : If after separating the points for variables from the solution of differential equation and then the plot will not be smooth when compared to the plot which is obtained by directly giving the solution of differential equation as an argument for the plot command.  
Issue-2 : Also, the plot becomes disjoint(it was meant to happen) i.e. from where the NaN is placed in the data to the next point. The point before NaN needs to be repeated after the placement of NaN so that plot remains connected.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 7, 2021, 7:33pm UTC](https://discourse.julialang.org/t/arrows-on-trajectory-as-it-evolves/60733/6 "2021-05-07T19:33:07Z")

</div>

It sounds like you know about the possible solutions already. You can take one of those and use it as a basis to write exactly the code you need.

If you specify the question more precisely, including a minimal example of what you have already tried, then we can be of more help.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 7, 2021, 8:35pm UTC](https://discourse.julialang.org/t/arrows-on-trajectory-as-it-evolves/60733/7 "2021-05-07T20:35:01Z")

</div>

The following code puts arrows on top of a parametric curve (x(t),y(t)) in 2D.  
It can be easily modified for 3D and/or for arc-length parameterization.

```julia
using Dierckx

# returns N points and N scaled-gradients along (xt,yt) equispaced on t
function arrowedcurve(xt,yt,N::Int)
    Nt = length(xt)
    tn = LinRange(1,Nt,N+2)[2:end-1]
    xt, yt = reshape(xt,1,:), reshape(yt,1,:) # because Dierckx is picky
    spl = ParametricSpline(1:Nt,[xt;yt], k=1) # use linear if plot code uses linear segments
    zn, dzn = evaluate(spl,tn)', derivative(spl, tn, nu=1)'
    cf = maximum([diff([extrema(xt)...]), diff([extrema(yt)...])])[1]/100
    dzn = cf * dzn ./ sqrt.(sum(abs2,dzn,dims=2))
    return zn, dzn
end

using Plots; gr()
t = 0:0.02:4π
xt, yt = t .* cos.(t), t .* sin.(t)

zn, dzn = arrowedcurve(xt,yt,12)
plot(xt,yt, c=:red, legend=false, ratio=1)
quiver!(zn[:,1],zn[:,2], quiver=(dzn[:,1],dzn[:,2]), c=:blue)

```

![curve_with_arrows_using_Dierckx](https://global.discourse-cdn.com/julialang/original/3X/9/d/9d7f44538bb7da8bbfa11680a349012d3369e6a2.png)

For equispaced arrows:  
 ![curve_with_arrows_using_Dierckx2](https://global.discourse-cdn.com/julialang/original/3X/7/7/77f3b6d06510d6cf42dcc59d559db38162e5ae47.png)
