6

I'm new on Julia and I've tried to make the code of following post How to plot a vector field in Julia?, but, didn't work, so, I would like to know if it's possible to plot with the package "Plots" and how? It'll be very important to my research.

P.s.: Someone gave to me the following code, but, actually, I don't know why isn't working:

using Plots
gr(size=(600,400))

function example()
  X = linspace(-2, 2, 100)
  Y = linspace(-2, 2, 100)
  f(x, y) = x^3 - 3x + y^2
  contour(X, Y, f)

  x = linspace(-2, 2, 11)
  y = linspace(-2, 2, 11)
  df(x, y) = [3x^2 - 3; 2y] / 25
  quiver!(x, y', quiver=df, c=:blue)

  xlims!(-2, 2)
  ylims!(-2, 2)
  png("example")
end

example()
rickhg12hs
  • 10,638
  • 6
  • 24
  • 42
  • Does this [previous SO post](https://stackoverflow.com/questions/51466537) help? – rickhg12hs Sep 17 '18 at 05:40
  • BTW, it's helpful if you elaborate "isn't working" more. For example, there are error messages (and show them), the plot doesn't look right, there is no plot, etc. – rickhg12hs Sep 17 '18 at 05:45
  • We need more information. For example, if you are on Julia 1.0 you will get errors because `linspace` doesn't exist anymore (one should use `range` from now on) which is completely unrelated to plotting. – carstenbauer Sep 17 '18 at 06:47

3 Answers3

12

As has already been said in the comments, you should provide error messages as well as people otherwise have to guess what's wrong with your code.

However, in your case I think I could guess it :)

On Julia 1.0 the following works:

using Plots
gr(size=(600,400))

function example()
  X = range(-2, stop=2, length=100)
  Y = range(-2, stop=2, length=100)
  f(x, y) = x^3 - 3x + y^2
  contour(X, Y, f)

  x = range(-2, stop=2, length=11)
  y = range(-2, stop=2, length=11)
  df(x, y) = [3x^2 - 3; 2y] / 25
  quiver!(x, y', quiver=df, c=:blue)

  xlims!(-2, 2)
  ylims!(-2, 2)
  png("example")
end

example()

and gives the following output

enter image description here

Note that I only changed all occurences of linspace to range(-2, stop=2, length=X) because the linspace function has been deprecated in Julia 0.7.

carstenbauer
  • 9,817
  • 1
  • 27
  • 40
  • Thanks for this great answer! I've been trying to reproduce your example (in order to optimize my own vectorfield plotting functions), but it's not working on Julia 1.5.3 (`quiver()` expects `y` each column to contain 11 rows). I'm not sure what developments Julia has undergone between when you answered and now--does Plots no longer broadcast the input data as it did when you posted this? I'd love to get your elegant solution to work again. Thanks! – Chris Brendel Feb 06 '21 at 17:51
4

For Plots v1.10.4, you need to provide a start position for every quiver

# same code as in above answer
quiver!(repeat(x,11), vec(repeat(y',11)), quiver=df, c=:blue)
wildart
  • 99
  • 1
3

Updating the answer to Julia 1.6.3,

Helper function source: How to plot a vector field in Julia?

using Plots
gr(size=(600,400))

meshgrid(x, y) = (repeat(x, outer=length(y)), repeat(y, inner=length(x))) # helper function to create a quiver grid.

function example()
  X = range(-2, stop=2, length=100)
  Y = range(-2, stop=2, length=100)
  f(x, y) = x^3 - 3x + y^2
  Plots.contour(X, Y, f)

  xs,ys = meshgrid(range(-2, stop=2, length=11), range(-2,stop=2,length=11))
  df(x, y) = [3x^2 - 3; 2y] / 25
  quiver!(xs, ys, quiver=df, c=:blue)

  xlims!(-2, 2)
  ylims!(-2, 2)
  png("example")
end

example()

enter image description here

BuddhiLW
  • 608
  • 3
  • 9