# 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

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)

In Plots quiver is implemented as a recipe which converts the x, y and quiver arguments to coordinates for the arrows separated by NaNs (https://github.com/JuliaPlots/Plots.jl/blob/master/src/recipes.jl#L874-L971). 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!