# Plotting vector field

I am trying to learn to plot a 2d vector field. I try to follow the example here.

using Plots
plotly()

meshgrid(x, y) = (repeat(x, outer=length(y)), repeat(y, inner=length(x)))

x, y = meshgrid(0.0:0.2:2.0, 0.0:0.2:2.0)
u = cos.(x) .* y
v = sin.(x) .* y # orginal had v = @. sin(x) * y but it looks like that's outdated? I changed it to this

quiver(x, y, quiver=(u, v))


If instead of quiver(x, y, quiver=(u, v)), I try quiver(x,y,u,v) as per the Matplotlib documentation, I get "Couldn’t process recipe args: (Array{Float64,1}, Array{Float64,1}, Array{Float64,1}, Array{Float64,1})
"
Full error

At first I had some ints mixed with floats, but making everything float didn’t help.

Related questions: 1 2

Any thoughts? thanks.

1 Like

I you don’t need to use Plots.jl, this can be easily reproduced with Gaston:

using Gaston
meshgrid(x, y) = (repeat(x, outer=length(y)), repeat(y, inner=length(x)))
x, y = meshgrid(0.0:0.2:2.0, 0.0:0.2:2.0)
u = 0.15 * cos.(x) .* y
v = 0.15 * sin.(x) .* y
plot(x, y, supp = [u v], w = :vectors) 2 Likes

With a couple caveats (only 1 matters), that worked thanks!

1. It didn’t work in Jupyterlab. It only worked in Jupyter notebook.
it just gives a little image icon below the cell but no output. I found a thread about a related python problem here. People were saying you need to use %matplotlib inline . Not sure if that’s applicable to julia or what that means.

2. It printed an unknown path at the top 3. I had to install GNUPlot

This example works fine with Plots with the GR backend.
Just remove plotly().

Note that matplotlib has nothing to do with Plots.jl, so it’s not expected that the syntax is necessarily the same. (Although the syntax in Plots.jl could be said to be unnecessarily complicated.)

Also, I’m not sure what you mean by a “2x2” vector field.

EDIT: It may be a good idea to normalise the vectors you’re plotting to all have the same length.

1 Like

I’m glad it worked! Regarding the caveats:

1. I haven’t tested Gaston with Jupypterlab. Feel free to file an issue.

2. Gnuplot uses the (temporary) file name where Gaston stores the plotting data as a legend. It can be supressed by default; see here.