# How to draw a line with an arrow in Plot.jl?

**URL:** https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974
**Category:** New to Julia
**Tags:** question, plotting
**Created:** [February 25, 2021, 1:10am UTC](https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974 "2021-02-25T01:10:01Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![ishihama](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@ishihama](https://discourse.julialang.org/u/ishihama)
#### Post date: [February 25, 2021, 1:10am UTC](https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974/1 "2021-02-25T01:10:02Z")

</div>

I wonder how to draw a line with an arrow in Plot.jl. The backend I use is GR, but any backend is OK to me. In the case of GnuPlot, it looks like `set arrow 1 from 0,9 to 0,-9`. Note, I do not mean I want to extend arrows from datapoints.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [February 25, 2021, 1:17am UTC](https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974/2 "2021-02-25T01:17:23Z")

</div>

> <https://stackoverflow.com/questions/58219191/drawing-an-arrow-with-specified-direction-on-a-point-in-scatter-plot-in-julia>

Of course you can draw the arrows where you want just providing a different set of vectors than those of the data (as opposed to the example given).

There is also this:

```julia
plot!([0,0],[0,1.1],arrow=true,color=:black,linewidth=2,label="")

```

Actually that is what I used in this example:

> <https://github.com/m3g/CKP/blob/master/disciplina/codes/velocities.jl>

Which produces:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/3/63dc6fff60bcff4b0b8c3166f99dde661ba0152a.png)

---

<div class="post-metadata">

### Author: ![ishihama](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@ishihama](https://discourse.julialang.org/u/ishihama)
#### Post date: [March 1, 2021, 10:22am UTC](https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974/3 "2021-03-01T10:22:13Z")

</div>

thank you very much!

---

<div class="post-metadata">

### Author: ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)
#### Post date: [March 28, 2021, 11:28pm UTC](https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974/4 "2021-03-28T23:28:13Z")

</div>

Hi @lmiq, I have a problem with arrows, which I know how to deal with in LaTeX but not in Julia.

The problem is this: usually if we want a connection between two points (both of which display a marker), the arrow overlaps a bit with the markers. In LaTeX we can say how much distance we want to be kept between the arrow and the marker by typing, e.g., `shorten> = 0.09cm`. Do you know how we can do this in Plots.jl? I have looked at the documentation, but there is no information about this issue.

An MWE that shows what I have in mind:

```julia
x = [1, 3 , NaN, 0.5, 1.0]
y = [1, 0.4, NaN, 1.5, 0.8]
GR.setarrowsize(1)
plot(x,y, marker =:circle, arrow=(:closed, 2.0))

```

The output is

 ![aa1](https://global.discourse-cdn.com/julialang/original/3X/2/4/24ec43ea0b4621e83f05ac0ec58b159f8dadfca0.png)

The overlapping between arrows and markers can be easily avoided in LaTeX, like this

 ![aa2](https://global.discourse-cdn.com/julialang/original/3X/4/3/43752ae205784ff6c522d0fa19c8e27f3c042092.png)

In simple cases, the overlapping is not a big deal; but the overlapping is quite annoying in more elaborate plotting.

Thanks a lot.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [March 28, 2021, 11:39pm UTC](https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974/5 "2021-03-28T23:39:32Z")

</div>

> [@VivMendes](#):
>
> avoided in LaTeX, like this

I don’t know.

But if you are familiar with plotting in latex (I’m not), why not using [pgfplots](https://nbviewer.jupyter.org/github/JuliaTeX/PGFPlots.jl/blob/master/doc/PGFPlots.ipynb)?

---

<div class="post-metadata">

### Author: ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)
#### Post date: [March 29, 2021, 12:12am UTC](https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974/6 "2021-03-29T00:12:43Z")

</div>

Besides Plots.jl I also use PGFPlots.jl and PGFPlotsX.jl. The last two packages are wrappers for the pgfplots.sty package, and they quite good. However, the introduction of LaTeX raw code into these wrappers is not always that simple. PGFPlots is a larger wrapper but only allows for raw LaTeX code in the preamble (as far as I know), while PGFPlotsX is more flexible but has quite a smaller amount of plotting examples available in Julia. Using pgfplots.sty is not the best option if we are dealing with data or numerical analysis (optimization, dynamic processes, …), for example.

Thanks a lot.

---

<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: [March 29, 2021, 7:48am UTC](https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974/7 "2021-03-29T07:48:24Z")

</div>

Would a display like here below be of your liking?

(i) with variable arrow head sizes:  
 ![arrows_bounded_by_circles_rg](https://global.discourse-cdn.com/julialang/original/3X/b/a/baf1e4e10657b66e6971e0b179f3f55063e87823.png)  
or  
(ii) with fixed arrow head sizes?  
 ![fixed_size_arrows_bounded_by_circles_rg](https://global.discourse-cdn.com/julialang/original/3X/c/c/cc068b59446de4ff8549451b957a8e9443375e0c.png)

---

<div class="post-metadata">

### Author: ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)
#### Post date: [March 29, 2021, 9:36am UTC](https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974/8 "2021-03-29T09:36:52Z")

</div>

@rafael.guerra that looks good. How do you do it? I tried this (overlapping still in):

```julia
x = [1, 3 , NaN, 0.5, 1.0]
y = [1, 0.4, NaN, 1.5, 0.8]
#GR.setarrowsize(1)
plot(x,y, marker =:circle, arrow=true, arrowsize=0.5)
#arrow=(:closed, 2.0))

```

 ![aa3](https://global.discourse-cdn.com/julialang/original/3X/a/7/a7e9c24ed75365d24d5269d2c7d4573ce6525880.png)

---

<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: [March 29, 2021, 9:50am UTC](https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974/9 "2021-03-29T09:50:06Z")

</div>

@VivMendes, please see code below which connects a sequence of dots by arrows without overlap, more in line with the Latex example you provided. The comments explain the different parameters.

If you have disconnected sequences, then need to run the code for each of them. One could write a function to handle that case if needed.

```julia
# Draw sequence of dots as circles connected by arrows without overlap

using LinearAlgebra, Plots; gr(legend=false)

# as: arrow head size 0-1 (fraction of arrow length; if <0 : use quiver with default constant size
# la: arrow alpha transparency 0-1
function arrow0!(x, y, u, v; as=0.07, lc=:black, la=1) # by @rafael.guerra
    if as < 0
        quiver!([x],[y],quiver=([u],[v]), lc=lc, la=la) # NB: better use quiver directly in vectorial mode
    else
        nuv = sqrt(u^2 + v^2)
        v1, v2 = [u;v] / nuv, [-v;u] / nuv
        v4 = (3*v1 + v2)/3.1623 # sqrt(10) to get unit vector
        v5 = v4 - 2*(v4'*v2)*v2
        v4, v5 = as*nuv*v4, as*nuv*v5
        plot!([x,x+u], [y,y+v], lc=lc,la=la)
        plot!([x+u,x+u-v5[1]], [y+v,y+v-v5[2]], lc=lc, la=la)
        plot!([x+u,x+u-v4[1]], [y+v,y+v-v4[2]], lc=lc, la=la)
    end
end

function circleShape(x,y,r) # by @lazarusA
    θ = LinRange(0,2π,72)
    x .+ r*sin.(θ), y .+ r*cos.(θ)
end

# support points (x,y) sorted in desired plotting order
x, y = [0.5, 1., 3.], [1.5, 1.0, 0.4]
# compute full-length connecting vectors
u, v = diff(x), diff(y)

# define circle radius in plot units
cr = 0.025

# recompute vector lengths to not overlap the circles
lv = [norm([u,v]) for (u,v) in zip(u,v)]
lv0 = lv .- 2*cr
u0, v0 = u .* lv0./lv, v .* lv0./lv

# recompute support points to start after circle
x0, y0 = x[1:end-1] .+ cr*u./lv, y[1:end-1] .+ cr*v./lv

# plot data
plot()
for (x,y) in zip(x,y)
   display(plot!(circleShape(x,y,cr),seriestype=:shape,c=:blue,lw=0.1,lc=:blue,ratio=1,fill_alpha=0.5))
end
for (x,y,u,v) in zip(x0,y0,u0,v0)
    display(arrow0!(x, y, u, v; as=-0.07, lc=:blue, la=1)) # if as > 0, variable arrow head sizes
end

```

---

<div class="post-metadata">

### Author: ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)
#### Post date: [March 29, 2021, 9:57am UTC](https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974/10 "2021-03-29T09:57:23Z")

</div>

@rafael.guerra Thanks a lot. That will be very useful.

---

<div class="post-metadata">

### Author: ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)
#### Post date: [March 29, 2021, 8:23pm UTC](https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974/11 "2021-03-29T20:23:43Z")

</div>

@rafael.guerra It works, but your code seems to have been developed for version 0.6. In Julia 1.5 we need to add `using LinearAlgebra`; otherwise `norm` will not be defined.

---

<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: [March 29, 2021, 8:29pm UTC](https://discourse.julialang.org/t/how-to-draw-a-line-with-an-arrow-in-plot-jl/55974/12 "2021-03-29T20:29:53Z")

</div>

@VivMendes, sorry this and other key packages are defined in my Julia `startup.jl`, and so, they are pre-loaded when coding and overlooked (I am using Julia 1.6.0 on Win10). Edited code above. Thanks.
