# Plotting vector field

**URL:** <https://discourse.julialang.org/t/plotting-vector-field/45393>\
**Category:** New to Julia\
**Tags:** plotting, diffeq, plots, gaston\
**Created:** [August 22, 2020, 11:35pm UTC](https://discourse.julialang.org/t/plotting-vector-field/45393 "2020-08-22T23:35:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)\
**Post date:** [August 22, 2020, 11:35pm UTC](https://discourse.julialang.org/t/plotting-vector-field/45393/1 "2020-08-22T23:35:36Z")

</div>

I am trying to learn to plot a 2d vector field. I try to follow the example [here](https://stackoverflow.com/questions/51466537/how-to-plot-a-vector-field-in-julia).

```julia
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))

```

I get “access to undefined reference”. full [error](https://paste.gg/p/anonymous/57a3e43279984cad97691d30d1dd6476)

If instead of `quiver(x, y, quiver=(u, v))`, I try `quiver(x,y,u,v)` as per the Matplotlib [documentation](https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.quiver), I get "Couldn’t process recipe args: (Array{Float64,1}, Array{Float64,1}, Array{Float64,1}, Array{Float64,1})  
"  
Full [error](https://paste.gg/p/anonymous/c344f3b8abe043a1bae17ca97d881e7e)

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

Related questions: [1](https://discourse.julialang.org/t/plotting-vector-fields/32704) [2](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208/3)

Any thoughts? thanks.

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [August 23, 2020, 12:22am UTC](https://discourse.julialang.org/t/plotting-vector-field/45393/2 "2020-08-23T00:22:49Z")

</div>

I you don’t need to use `Plots.jl`, this can be easily reproduced with [Gaston](https://github.com/mbaz/Gaston.jl):

```julia
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)

```

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

---

<div class="post-metadata">

**Author:** ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)\
**Post date:** [August 23, 2020, 1:08am UTC](https://discourse.julialang.org/t/plotting-vector-field/45393/3 "2020-08-23T01:08:08Z")

</div>

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.  
 ![Screen Shot 2020-08-22 at 7.49.09 PM](https://global.discourse-cdn.com/julialang/original/3X/2/7/27fd41360cd71c1dc604d497c03c27e3da28fef9.png)  
I found a thread about a related python problem [here](https://github.com/jupyter/notebook/issues/3523). 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  
 ![Screen Shot 2020-08-22 at 7.48.47 PM](https://global.discourse-cdn.com/julialang/original/3X/3/c/3c0c706e7aa8ed30572f2b80713aec64a6237fe4.png)

3. I had to install GNUPlot

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 23, 2020, 1:14am UTC](https://discourse.julialang.org/t/plotting-vector-field/45393/4 "2020-08-23T01:14:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [August 23, 2020, 2:28am UTC](https://discourse.julialang.org/t/plotting-vector-field/45393/5 "2020-08-23T02:28:26Z")

</div>

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](https://mbaz.github.io/Gaston.jl/stable/#Gnuplot-configuration-1).

3. See [https://github.com/mbaz/Gaston.jl/issues/135](https://github.com/mbaz/Gaston.jl/issues/135)
