# Makie plot with arrows to follow the trajectory of optimization solver

**URL:** https://discourse.julialang.org/t/makie-plot-with-arrows-to-follow-the-trajectory-of-optimization-solver/127612
**Category:** General Usage
**Tags:** question, plotting
**Created:** [April 2, 2025, 12:33am UTC](https://discourse.julialang.org/t/makie-plot-with-arrows-to-follow-the-trajectory-of-optimization-solver/127612 "2025-04-02T00:33:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)
#### Post date: [April 2, 2025, 12:33am UTC](https://discourse.julialang.org/t/makie-plot-with-arrows-to-follow-the-trajectory-of-optimization-solver/127612/1 "2025-04-02T00:33:49Z")

</div>

I am trying write a notebook to display some nonlinear optimization descent solvers. Using `CairoMakie`, I wanted to make a plot that looks something like this.

 ![grad_descent](https://global.discourse-cdn.com/julialang/original/3X/f/a/fa4524980c068c7319720d9d99ff10812bed8d0f.png)

The arrows should have their tail at the current point in the descent solver and the head at the subsequent point. I have been able to plot a simple contour plot, but I was not sure how to  
get these lines to work? Like would this be a a regular line plot or an arrow plot?

```julia
using CairoMakie
using LinearAlgebra

h(x) = x'*[2 0; 2 5]*x

xs = LinRange(-5, 5, 100)
ys = LinRange(-5, 5, 100)
zs = [h([x;y]) for x in xs, y in ys]
contour(zs, levels = -1:5:100)

```

This looks like the plot below.

 ![test](https://global.discourse-cdn.com/julialang/original/3X/3/1/313351d0df0221e187a0e354d83a86eb05ffe29f.png)

So I just need a little nudge on how to get the following points with the arrows  
into the plot. I did not include code for generating these points since it would make the MWE a little  
more than minimal. But I can always add that in if people request it. I also made the function really simple but I have more complicated functions in reserve that will take multiple steps to reach the minimum.

```julia
 (1, [10.2, 4.78])
 (2, [0.0, 0.0])

```

---

<div class="post-metadata">

### Author: ![kmiernik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kmiernik/32/202783_2.png) [@kmiernik](https://discourse.julialang.org/u/kmiernik)
#### Post date: [April 2, 2025, 6:52am UTC](https://discourse.julialang.org/t/makie-plot-with-arrows-to-follow-the-trajectory-of-optimization-solver/127612/2 "2025-04-02T06:52:26Z")

</div>

Will something like this do the trick?

```julia
using GLMakie

# This is fake data for contour plot
N(x, y; μx=0, σx=1, μy=0, σy=1, ρ=0) = @. 1 / (2*π*σx*σy*sqrt(1-ρ^2)) * exp( -1/(2*(1-ρ^2)) * ( (x-μx)^2/σx^2 - 2*ρ*(x-μx)/σx*(y-μy)/σy + (y-μy)^2/σy^2 ) )
x = -2:0.05:2
y = -2:0.05:2

# This is fake data for points (arrow tails)
th = acos.(1 .- 2 .* rand(6))
ph = rand(6) .* 2 .* π
r = 2 .* exp.(collect(0:-0.2:-1.0) ./ 0.25)
xa = r .* sin.(th) .* cos.(ph)
ya = r .* sin.(th) .* sin.(ph)

# Arrow directions
u = xa[2:end] .- xa[1:end-1]
v = ya[2:end] .- ya[1:end-1]

fig = Figure(size=(800, 600))
ax = Axis(fig[1, 1])

contour!(ax, x, y, N(x, y'; ρ=0.5))
scatter!(ax, xa, ya; color=:black, markersize=4)
# Arrows are little shorter so head and tails do not overlap
arrows!(ax, xa[1:end-1], ya[1:end-1], u .* 0.95, v .* 0.95)

fig

```

 ![arrows](https://global.discourse-cdn.com/julialang/original/3X/0/3/0306ba9c64567fc7fcc17d5aa6f49dc4da6bc1dd.png)

For more detailed options for `arrows` see [arrows | Makie](https://docs.makie.org/stable/reference/plots/arrows#MakieCore.arrows-reference-plots-arrows)

---

<div class="post-metadata">

### Author: ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)
#### Post date: [April 2, 2025, 7:05am UTC](https://discourse.julialang.org/t/makie-plot-with-arrows-to-follow-the-trajectory-of-optimization-solver/127612/3 "2025-04-02T07:05:24Z")

</div>

Oh yes, so it is a combination of `contour`, `scatter`, and `arrows`. Okay, I will study this a bit, but it looks like this will work. Excellent. Thanks so much for the example. I was not sure how to get that next step.

---

<div class="post-metadata">

### Author: ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)
#### Post date: [April 2, 2025, 6:30pm UTC](https://discourse.julialang.org/t/makie-plot-with-arrows-to-follow-the-trajectory-of-optimization-solver/127612/4 "2025-04-02T18:30:16Z")

</div>

Maybe some arrows from [MakieExtra](https://juliahub.com/ui/Packages/General/MakieExtra)?
