Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3D Vector Plot in Julia

Tags:

plot

julia

I am trying to plot an EM wave (propagating in the z-direction) vector field in Julia. I looked around and it looks like quiver is what I need to use and I have tried that with unsuccessful results. As far as I understand (x, y, z) are the origins of the vectors and (u, v, w) are the vectors themselves originating at the (x, y, z) points. Here is what I have so far but this doesn't seem to produce the correct plot. How can I get this to work? I'm open to try other plotting libs as well. Thanks in advance.

using Plots; gr()
t = 0; n = 100; k = 1; ω = 1; φ = π/4
x = y = w = zeros(n)
z = range(0, stop=10, length=n)
u = @. cos(k*z - ω*t)
v = @. sin(k*z - ω*t)
quiver(x, y, z, quiver=(u, v, w), projection="3d")

Output

like image 316
uncertain-berg Avatar asked Dec 20 '18 15:12

uncertain-berg


1 Answers

I'm not exactly sure this is the result you want but I've managed to make your code work in Julia v1.1 :

using PyPlot

pygui(true)

fig = figure()
ax = fig.gca(projection="3d")
t = 0; n = 100; k = 1; ω = 1; φ = π/4
x = y = w = zeros(n)
z = range(0, stop=10, length=n)
u = cos.(k*z .- ω*t)
v = sin.(k*z .- ω*t)
ax.quiver(x,y,z, u,v,w)

enter image description here

Or, with colors :

using PyPlot
using Random

function main()
    pygui(true)

    fig = figure()
    ax = fig.gca(projection="3d")
    t = 0; n = 100; k = 1; ω = 1; φ = π/4
    x = y = w = zeros(n)
    z = range(0, stop=10, length=n)
    u = cos.(k*z .- ω*t)
    v = sin.(k*z .- ω*t)
    a = ((u[1], 0.8, 0.5), (u[2], 0.8, 0.5))
    for i in 3:length(u)-2
        a = (a..., (abs(u[i]), 0.8, 0.5))
    end
    c = ((0.4, 0.5, 0.4), (0.4, 0.9, 0.4), (0.1, 0.1, 0.1))
    q = ax.quiver(x,y,z, u,v,w, color = a)
end
main()

enter image description here

like image 55
JKHA Avatar answered Sep 16 '22 23:09

JKHA