# How to plot quiver plot over heatmap

**URL:** <https://discourse.julialang.org/t/how-to-plot-quiver-plot-over-heatmap/66000>\
**Category:** General Usage\
**Tags:** plotting, plots\
**Created:** [August 7, 2021, 9:58pm UTC](https://discourse.julialang.org/t/how-to-plot-quiver-plot-over-heatmap/66000 "2021-08-07T21:58:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![meck](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@meck](https://discourse.julialang.org/u/meck)\
**Post date:** [August 7, 2021, 9:58pm UTC](https://discourse.julialang.org/t/how-to-plot-quiver-plot-over-heatmap/66000/1 "2021-08-07T21:58:54Z")

</div>

I am trying to plot a quiver plot (a vector field) over a heatmap. My only reference after one day of research is this old notebook: [https://github.com/JuliaPlots/ExamplePlots.jl/blob/master/notebooks/quiver.ipynb](https://github.com/JuliaPlots/ExamplePlots.jl/blob/master/notebooks/quiver.ipynb)

My function is

` initial_c(x, y, z) = (exp(-x^2 - y^2))`

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/d/cdbd76408e7922fc92e81f478fa902537b35f1f1.png)

Is there a way to do this and even print out the values of every vector for the respective grid point?

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 11, 2021, 1:27pm UTC](https://discourse.julialang.org/t/how-to-plot-quiver-plot-over-heatmap/66000/2 "2021-08-11T13:27:02Z")

</div>

Plots.jl examples

Example 1:

```julia
using ForwardDiff
using Plots

f(x, y) = exp(-(x^2 + y^2))
u(x, y) = ForwardDiff.derivative(Base.Fix2(f, y), x) # = f_x(x, y)
v(x, y) = ForwardDiff.derivative(Base.Fix1(f, x), y) # = f_y(x, y)

xlim = (-1.7, 1.7)
ylim = (-1.7, 1.7)
xs = range(xlim...; length=200)
ys = range(ylim...; length=200)

c = 0.5
x = range(-1.5, 1.5; length=11)
y = range(-1.5, 1.5; length=11)
X, Y = reim(complex.(x', y)) # meshgrid
U, V = c*u.(x', y), c*v.(x', y)

heatmap(xs, ys, f)
quiver!(vec(X-U/2), vec(Y-V/2); quiver=(vec(U), vec(V)), color=:cyan)
plot!(; xlim, ylim, size=(450, 400))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/7/57623025c009cdfdb484aaa7a69c6710ed0fdb90.jpeg)

Example2:

```julia
using ForwardDiff
using Plots

f(x, y) = y * exp(-(x^2 + y^2))
u(x, y) = -ForwardDiff.derivative(Base.Fix2(f, y), x) # = f_x(x, y)
v(x, y) = -ForwardDiff.derivative(Base.Fix1(f, x), y) # = f_y(x, y)

xlim = (-2.2, 2.2)
ylim = (-2.2, 2.2)
xs = range(xlim...; length=200)
ys = range(ylim...; length=200)

c = 0.5
x = range(-2.0, 2.0; length=13)
y = range(-2.0, 2.0; length=13)
X, Y = reim(complex.(x', y)) # meshgrid
U, V = c*u.(x', y), c*v.(x', y)

heatmap(xs, ys, f)
quiver!(vec(X-U/2), vec(Y-V/2); quiver=(vec(U), vec(V)), color=:cyan)
plot!(; xlim, ylim, size=(450, 400))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/f/ef483350899aabab8af5683f856e3bb350724293.jpeg)

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [August 2, 2022, 4:59am UTC](https://discourse.julialang.org/t/how-to-plot-quiver-plot-over-heatmap/66000/3 "2022-08-02T04:59:31Z")

</div>

Can I ask why the second example has

```julia
u(x, y) = -ForwardDiff.derivative(Base.Fix2(f, y), x) # = f_x(x, y)
v(x, y) = -ForwardDiff.derivative(Base.Fix1(f, x), y) # = f_y(x, y)

```

why you add the minus?

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [August 2, 2022, 11:23am UTC](https://discourse.julialang.org/t/how-to-plot-quiver-plot-over-heatmap/66000/4 "2022-08-02T11:23:40Z")

</div>

For the first one the quiver represents the gradient vector field, grad(f), while in the second one minus gradient. Most likely @genkuroki made this choice for variation or to illustrate that grad(f), respectively -grad(f) is the direction of steepest ascent/descent.
