Quiver colors in Plots.jl

Hi all,

I made this simple example where I plot a bunch of arrows

using Plots

# Arrow position.
x = collect(0:10)
y = fill(0., 10)
# Arrow direction.
u = fill(1., 10)
v = fill(1., 10)
# Arrow color.
c = collect(1:10)

plt = quiver(x, y, quiver=(u, v), line_z=c, c=:viridis)
display(plt)

which outputs this graphic
out

If I understand correctly, the colors of the arrows should be sorted from left to right from black to yellow, however the colors appear in cycles. How can I do this correctly? (I can do this in PyPlot.jl, but would prefer Plots.jl)

Thanks in advance :grinning:

In Plots quiver is implemented as a recipe which converts the x, y and quiver arguments to coordinates for the arrows separated by NaNs (Plots.jl/recipes.jl at master · JuliaPlots/Plots.jl · GitHub). In your code above you can take a look at the resulting x (and y) coordinates with:

julia> plt[1][1][:x]
44-element Array{Float64,1}:
 NaN  
   0.0
   1.0
 NaN
 NaN
   1.0
   2.0
   ⋮
  10.0
 NaN
 NaN
  10.0
  11.0
 NaN

(Actually we should only separate the lines with a single NaN instead of two here.)
For this data we want to change the color after every four elements. So you can “fix” your plot with:

plt = quiver(x, y, quiver=(u, v), line_z=repeat(c, inner=4), c=:viridis)
2 Likes

Thanks! It is something I wasn’t able to find in the documentation, but it works! :grinning:

For me, it doesn’t work when using inner=4 (“ERROR: BoundsError: attempt to access 40-element Vector{Int64} at index [42]”), but inner=5 is ok.
In addition, why x is 11 elements vector and y is 10 elements vector?

    # Arrow position.
    x = collect(0:10) 
    y = fill(0.0, 10)
    # Arrow direction.
    u = fill(1.0, 10)
    v = fill(1.0, 10)
    # Arrow color.
    c = collect(1:10)
    quiver(x, y, quiver=(u, v), line_z=repeat(c, inner=5), c=:vik, linewidth=3)