I wrote this down because it is weirdly hard to find anything on this topic.
To plot vector fields, use the undocumented (as of 2019 Dec 26) quiver function. Quiver is a strange name, coming from the military: a “quiver” is “a bundle of arrows”.
quiver(x,y,quiver=(u,v))
quiver!(x,y,quiver=(u,v))
Make a quiver (vector field) plot. The ith vector extends from (x[i],y[i]) to (x[i] + u[i], y[i] + v[i]).
Example
≡≡≡≡≡≡≡≡≡
julia> quiver([1,2,3],[3,2,1],quiver=([1,1,1],[1,2,3]))
Here is my code that does this job, even if uglily,
using Plots
function vectorfield2d(field, points, arrowlength=0.1)
# More input pattern parsing is solved by the Plots package, but I don't know how.
errormessage = "Incorrect formatting of points. Please format them as [x1 y1; x2, y2;...]"
if typeof(points) <: Array{<:Number, 2} && size(points)[1] === 2
vectors = similar(points)
for i in 1:size(points)[2]
vectors[:, i] .= collect(field(points[:, i]...))
end
else
error(errormessage)
end
vectors .*= arrowlength
quiver(points[1, :],points[2, :],quiver=(vectors[1, :], vectors[2, :]))
display(plot!())
end
vectorfield = vectorfield2d
A demonstration:
function meshgrid(n)
xs = ones(n) .* (1:n)'
ys = xs'
xys = permutedims(cat(xs, ys; dims = 3), [3, 1, 2])
return reshape(xys, 2, n^2)
end
vf(x, y) = (-y, x) # circular vectorfield
grid = meshgrid(20) ./ 2 .- [5; 5]
vectorfield2d(vf, grid)