# 3D quiver plot?

**URL:** <https://discourse.julialang.org/t/3d-quiver-plot/69075>\
**Category:** Visualization\
**Tags:** plotting, plots, makie, 3d\
**Created:** [October 1, 2021, 9:29pm UTC](https://discourse.julialang.org/t/3d-quiver-plot/69075 "2021-10-01T21:29:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [October 1, 2021, 9:29pm UTC](https://discourse.julialang.org/t/3d-quiver-plot/69075/1 "2021-10-01T21:29:20Z")

</div>

Hello!

I am aware of the quiver function, but am not sure how to easily do a 3d quiver plot?

Kind regards

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [October 1, 2021, 9:36pm UTC](https://discourse.julialang.org/t/3d-quiver-plot/69075/2 "2021-10-01T21:36:06Z")

</div>

[https://makie.juliaplots.org/stable/examples/plotting\_functions/arrows/#example\_2533070602242681981](https://makie.juliaplots.org/stable/examples/plotting_functions/arrows/#example_2533070602242681981)

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [October 1, 2021, 9:42pm UTC](https://discourse.julialang.org/t/3d-quiver-plot/69075/3 "2021-10-01T21:42:22Z")

</div>

Nice, thanks for letting me know.

I found this: [https://github.com/JuliaPlots/Plots.jl/pull/3134](https://github.com/JuliaPlots/Plots.jl/pull/3134)

And just tested, I was trying quiver3d, but basically just use quiver.

I don’t get any arrows though, which is annoying

Kind regards

---

<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:** [October 1, 2021, 10:06pm UTC](https://discourse.julialang.org/t/3d-quiver-plot/69075/4 "2021-10-01T22:06:22Z")

</div>

You could try the `arrow3d!` user function [posted here](https://discourse.julialang.org/t/plot-with-sticks-or-arrows/68422/14) (_NB: the code is posted below the figures_).

It allows displaying either fixed arrowhead sizes or having these varying proportionally to vector length.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [October 2, 2021, 6:54am UTC](https://discourse.julialang.org/t/3d-quiver-plot/69075/5 "2021-10-02T06:54:55Z")

</div>

Thanks Rafael!

Do you have a solution which is not done in Julia 1.7.0? Since it is not fully released right now, I prefer to stay on a older version (1.5). I get this bug:

```julia
ERROR: MethodError: no method matching svd!(::Array{Float64,1}; full=true, alg=LinearAlgebra.DivideAndConquer())

```

Kind regards

---

<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:** [October 2, 2021, 7:05am UTC](https://discourse.julialang.org/t/3d-quiver-plot/69075/6 "2021-10-02T07:05:39Z")

</div>

For older Julia version, please [try this](https://discourse.julialang.org/t/tube-plots/65999/9), to get one normal vector out of `nullspace()`

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [October 2, 2021, 7:15am UTC](https://discourse.julialang.org/t/3d-quiver-plot/69075/7 "2021-10-02T07:15:35Z")

</div>

Sorry, I am not getting it completely.

The code I have now is:

```julia
function arrow3d!(x, y, z, u, v, w; as=0.1, lc=:black, la=1, lw=0.4, scale=:identity)
    (as < 0) && (nv0 = -maximum(norm.(eachrow([u v w]))))
    for (x,y,z, u,v,w) in zip(x,y,z, u,v,w)
        nv = sqrt(u^2 + v^2 + w^2)
        v1, v2 = -[u,v,w]/nv, collect(eachcol(nullspace(permutedims([u,v,w]))))
        v4 = (3*v1 + v2)/3.1623 # sqrt(10) to get unit vector
        v5 = v4 - 2*(v4'*v2)*v2
        (as < 0) && (nv = nv0) 
        v4, v5 = -as*nv*v4, -as*nv*v5
        plot!([x,x+u], [y,y+v], [z,z+w], lc=lc, la=la, lw=lw, scale=scale, label=false)
        plot!([x+u,x+u-v5[1]], [y+v,y+v-v5[2]], [z+w,z+w-v5[3]], lc=lc, la=la, lw=lw, label=false)
        plot!([x+u,x+u-v4[1]], [y+v,y+v-v4[2]], [z+w,z+w-v4[3]], lc=lc, la=la, lw=lw, label=false)
    end
end

```

I am not really sure how to fix this, it is now saying:

```julia
ERROR: MethodError: no method matching +(::Float64, ::SubArray{Float64,1,Array{Float64,2},Tuple{Base.Slice{Base.OneTo{Int64}},Int64},true})
For element-wise addition, use broadcasting with dot syntax: scalar .+ array

```

I struggle with the math, else I would have done what I could to fix it.

Kind regards

---

<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:** [October 2, 2021, 7:50am UTC](https://discourse.julialang.org/t/3d-quiver-plot/69075/8 "2021-10-02T07:50:03Z")

</div>

> [@Ahmed\_Salih](#):
>
> `collect(eachcol(nullspace(permutedims([u,v,w]))))`

We need to collect just the first vector from the nullspace:

```julia
collect(eachcol(nullspace(permutedims([u,v,w]))))[1]

```

With this modification it works fine in Julia 1.6.2.

NB:  
In the time available now could not make it to work without using `collect()` (_@DNF to the rescue_) and really not sure why the alternative below errors for the examples linked:

```julia
first.(eachcol(nullspace(permutedims([u,v,w]))))

```

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [October 2, 2021, 7:54am UTC](https://discourse.julialang.org/t/3d-quiver-plot/69075/9 "2021-10-02T07:54:24Z")

</div>

> [@rafael.guerra](#):
>
> `collect(eachcol(nullspace(permutedims([u,v,w]))))[1]`

I am using what you suggest now, but it also seemed to me to work with using:

```julia
nullspace(permutedims([u, v, w]))[:,1]

```

And arrows were pointing in the same direction as in your pictures etc. Perhaps this is a way to do it without collect?

I did not think about it too much, I just tried until I got something which seemingly worked.

But thank you for the code! Makes my life much easier. Wish quiver had this functionality build-in though

Kind regards

---

<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:** [October 2, 2021, 8:07am UTC](https://discourse.julialang.org/t/3d-quiver-plot/69075/10 "2021-10-02T08:07:18Z")

</div>

PlotlyJS.jl provides a 3d quiver plot, called cone3d chart, and here are a few examples:

1. [https://nbviewer.jupyter.org/github/empet/3D-Viz-with-PlotlyJS.jl/blob/main/10-Cones-representing-a-vector-field.ipynb](https://nbviewer.jupyter.org/github/empet/3D-Viz-with-PlotlyJS.jl/blob/main/10-Cones-representing-a-vector-field.ipynb)
2. [https://nbviewer.jupyter.org/github/empet/3D-Viz-with-PlotlyJS.jl/blob/main/11-Cone-plots-in-a-volume.ipynb](https://nbviewer.jupyter.org/github/empet/3D-Viz-with-PlotlyJS.jl/blob/main/11-Cone-plots-in-a-volume.ipynb)

Reference: [Cone traces in Julia](https://plotly.com/julia/reference/cone/)

---

<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:** [October 2, 2021, 9:06am UTC](https://discourse.julialang.org/t/3d-quiver-plot/69075/11 "2021-10-02T09:06:05Z")

</div>

This makes me wonder why Robin Hood is still a legend:

 ![3D_Robin_Hood](https://global.discourse-cdn.com/julialang/original/3X/c/0/c053074388d98c1fa937c782e9a952c9754b4244.jpeg)
