# Quiver colors in Plots.jl

**URL:** <https://discourse.julialang.org/t/quiver-colors-in-plots-jl/34981>\
**Category:** Visualization\
**Tags:** question, plotting\
**Created:** [February 21, 2020, 3:35pm UTC](https://discourse.julialang.org/t/quiver-colors-in-plots-jl/34981 "2020-02-21T15:35:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alvrg](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@alvrg](https://discourse.julialang.org/u/alvrg)\
**Post date:** [February 21, 2020, 3:35pm UTC](https://discourse.julialang.org/t/quiver-colors-in-plots-jl/34981/1 "2020-02-21T15:35:14Z")

</div>

Hi all,

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

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/9/1/91eb27b7bd834760a8ae74057c58dea887893623.png)

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 😀

---

<div class="post-metadata">

**Author:** ![daschw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daschw/32/2926_2.png) [@daschw](https://discourse.julialang.org/u/daschw)\
**Post date:** [February 28, 2020, 10:31am UTC](https://discourse.julialang.org/t/quiver-colors-in-plots-jl/34981/2 "2020-02-28T10:31:29Z")

</div>

In Plots `quiver` is implemented as a recipe which converts the `x`, `y` and `quiver` arguments to coordinates for the arrows separated by `NaN`s ([Plots.jl/recipes.jl at master · JuliaPlots/Plots.jl · GitHub](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
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:

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

```

---

<div class="post-metadata">

**Author:** ![alvrg](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@alvrg](https://discourse.julialang.org/u/alvrg)\
**Post date:** [March 5, 2020, 9:37am UTC](https://discourse.julialang.org/t/quiver-colors-in-plots-jl/34981/3 "2020-03-05T09:37:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dk-zen](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dk-zen](https://discourse.julialang.org/u/dk-zen)\
**Post date:** [May 11, 2023, 7:04am UTC](https://discourse.julialang.org/t/quiver-colors-in-plots-jl/34981/5 "2023-05-11T07:04:18Z")

</div>

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?

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

```
