Anyone have a Plots quiver() example?

I’ve just spent a fruitless day looking through Plots, Plot.ly and PyPlot documentation, trying to find a prototype for the (2-D) quiver() diagram in Plots. I’ve discovered that it exists, and I’ve even achieved a contour diagram by entering:
quiver(1:10,1:10,rand(10,10))
But I really want a bunch of arrows, and really don’t want to fight my way any longer through the Plots code. Does anyone out there have a simple quiver example 2-dimensional flow? Thanks.

This example works with (pure) GR:

using GR
x = linspace(-2, 2, 21)
y = x
X, Y = meshgrid(x,y)
z = X.*exp(-X.^2 - Y.^2)
contour(x, y, z)
u, v = GR.gradient(x, y, z)
quiver(x, y, u, v)

We’ll have to ask @tbreloff - Plots seems to have another signature for the quiver() function …

1 Like

Thanks @jheinen. Yes, I’d already tried:
quiver(1:10,1:10,rand(10,10),rand(10,10)
and discovered the fourth parameter is the problem. I’d like if possible to stick to naked Plots code, since I’m composing a course for students. I’m intrigued though by the fact that GR doesn’t appear to need either a Matlab-like “hold on” or a Plots “quiver!”. How do you signal that the contours and the quiver are to apply to the same graphic?

Do a search for quiver here: https://juliaplots.github.io/attributes/

The call should look something like:

using Plots
# setup
quiver(x, y, quiver=(u,v))

The quiver keyword could be replaced by aliases: gradient, quiver2d, quivers, velocity.

Also you should be allowed to pass either a “vector of tuples” or a “tuple of vectors”, whichever is easier for you.

Also here is an old notebook that gives a full example… hopefully it still works :slight_smile:

2 Likes

Thanks very much, @tbreloff. However I fear my newbie status is proving a problem. This may be because I’m coming at the problem from a Matlab perspective.

  1. The attributes reference is an abstract description which I find difficult to understand without a concrete example.
  2. I’m unclear from your inline example “quiver(x, y, quiver=(u,v))” what exactly the various arguments are. From a Matlab point of view I’d expect x and y to be meshgrids, however I see that in the contour plots you use linspaces instead. When I use meshgrids I get a load of error messages, and when I use linspaces, I only get arrows on a leading diagonal of points (1,1), (2,2), … Should I be using tuples?
  3. In the first example in the notebook you only use a single position variable y.
  4. In the second notebook example you use vec(), which I’ve just looked up and understand, but not why you use it. You also use P2, which I can’t find in the documentation.

Help? :confused:

I’d love to have examples for every attribute, but I don’t have the time to build it. Volunteers?

Vectors in this case. The arrows don’t need to be on a special grid like some other frameworks… they’re just a list of points.

The x is implicitly filled in (like many other things in Plots)

This is why your meshgrids don’t work…they should just be vectors

This is: typealias P2 FixedSizeArrays.Vec{2,Float64}. Don’t worry about it… you don’t need it.


I just ran this example locally:

julia> using Plots

julia> u, v = rand(10),rand(10);

julia> quiver(rand(10), gradient=(u,v))

Since PyPlot is just a wrapper around Matplotlib, usually you google for Matplotlib examples and then translate the syntax from Python to Julia.

e.g. have you looked at the quiver documentation, which explains all of the parameters of quiver()?

1 Like

Gottit! Thank you both. I was indeed encumbered by Matlab thoughts: I thought rand(10) produced a (10x10) matrix, and I didn’t understand that the graphics commands require a vector of pairs. Unfortunately I haven’t yet worked out how to format code on this forum, but here is the small test module I’ve written, in case anyone wants to use it as a code example:

module Plotz
export plotz;

using Plots;

function plotz(
   f = (z->z*im), reRng = [-3,3], imRng = [-2,2], Δ = 0.5
   )
   #------------------------------------------------------
   # Plot the function f(z) over the complex range
   # (reRng x imRng) in steps of length delta.
   #------------------------------------------------------
   reRng = reRng[1]:Δ:reRng[2];
   imRng = imRng[1]:Δ:imRng[2];
   x = vec(repmat(reRng',length(imRng),1));
   y = vec(repmat(imRng, length(reRng),1));
   z = f(complex(x,y));
   z = Δ*z/maximum(abs(z));

   display( quiver(x,y,
      quiver=(real(z),imag(z)),aspect_ratio=1)
   );

   return;
end;

end
2 Likes

Looks very good on Julia 0.5.2 and like a hasty undergrad effort on Julia 0.6.0

@jheinen Great job with the GR package. I started to use it recently and it is amazingly fast. Thank you.

I am exploring the package functions to learn and practice their implementation. However, I am having troubles sometimes to get the plot displayed within a notebook cell. For instance, using the code you showed above my output is:

I used GR.show() to see whether the plot appears but to no avail. Could you please clarify to me what is wrong in this case? Another problem I am facing is that whenever I plot within a for-loop, the plot is not displayed at all. An example code is as follows:

figure(size=(800,600))
setwsviewport(0,1,0,1)
for i = 1:200
    plot(x, sin.(x + i / 10.0))
end```

My system info is attached below.

Julia Version 0.6.4
Commit 9d11f62 (2018-07-09 19:09 UTC)
Platform Info:
OS: Linux (x86_64-linux-gnu)
CPU: Intel(R) Core™ i7-7700HQ CPU @ 2.80GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell MAX_THREADS=16)
LAPACK: libopenblas64_
LIBM: libopenlibm
LLVM: libLLVM-3.9.1 (ORCJIT, broadwell)```

Thanks.

Mixing low-level functions and convenience methods is currently not supported in GR. You could use the hold function, but there seems to be a bug. I’ll check this next week …

The following example shows how to display in a loop:

using GR
import IJulia
x = linspace(0, 2*pi, 100) 
dt, nsteps = 0.03,100
for n = 1:nsteps
    IJulia.clear_output(true)
    plot(x, sin.(x - n*dt)) |> display
end

@jheinen The animation works. Thanks.