# Quiver plot (Plots.pyplot) with different colors depending on the length of the arrow

**URL:** <https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577>\
**Category:** Visualization\
**Tags:** plotting\
**Created:** [April 19, 2021, 4:49am UTC](https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577 "2021-04-19T04:49:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![hardy](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hardy](https://discourse.julialang.org/u/hardy)\
**Post date:** [April 19, 2021, 4:49am UTC](https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577/1 "2021-04-19T04:49:54Z")

</div>

Hello,  
I am new to Julia and tried to plot the gradient field of a loss function. I can get the lengths of the arrows by norm(dx, dy). But I don’t know how to convert a vector of values (lengths) into a vector of colors or how to specify a color based on a value.  
My plot order looks like this:  
""pl=quiver(Kp\_plot, Ki\_plot, quiver=(grad\_Kp, grad\_Ki), xlims=(Kp\_plot[1],Kp\_plot[end]) ,ylims=(Ki\_plot[1],Ki\_plot[end]) “”

 ![quiver_plot06.43.08](https://global.discourse-cdn.com/julialang/original/3X/c/3/c39fa1f0212229220b251e321f6bf124546b31e3.png)

I searched for hours and hope desperately that someone can help me

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 19, 2021, 5:13am UTC](https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577/2 "2021-04-19T05:13:14Z")

</div>

Please check [this post](https://discourse.julialang.org/t/quiver-colors-in-plots-jl/34981/2) out.

---

<div class="post-metadata">

**Author:** ![hardy](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hardy](https://discourse.julialang.org/u/hardy)\
**Post date:** [April 19, 2021, 6:07am UTC](https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577/3 "2021-04-19T06:07:38Z")

</div>

Thank you for this really quick response!  
I already saw this post, but didn’t quite understand the argument line\_z and how I could use it for my example. I found another code example with

> Blockquote  
> color = np.sqrt(((v-n)/2) **2 + ((u-n)/2)** 2)  
> quiver(X,Y,u,v,color, alpha=0.8)  
> Blockquote

but this doesn’t work as well. Some information is missing for me. ☹

But at least I found Blockquotes

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 19, 2021, 6:24am UTC](https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577/4 "2021-04-19T06:24:36Z")

</div>

Your last quote seems from Python.  
You may define an array of arrow lengths and feed it to `line_z` argument as done [here](https://discourse.julialang.org/t/plots-jl-assigning-seriescolor-using-colorgradient-for-line-plot/17099/9).  
Best is to post a MWE.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [April 19, 2021, 6:47am UTC](https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577/5 "2021-04-19T06:47:35Z")

</div>

I am not sure why I need 40 elements in line\_z but this seems to do the trick.  
` quiver(rand(10),rand(10),quiver=(rand(10),rand(10)),line_z=rand(40))`

```julia
using Plots
x = rand(10)
y = rand(10)
dx = rand(10)
dy = rand(10)
c = kron(sqrt.(dx .^2 .+dy .^2),[1,1,1,1])
quiver(x,y,quiver=(dx,dy),line_z=c)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 19, 2021, 7:53am UTC](https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577/6 "2021-04-19T07:53:49Z")

</div>

Another way, that might be a bit simpler but, in pyplot() the arrow-head colors do not look good while they are fine in gr() …

```julia
using Plots
x , y = rand(10), rand(10)
dx, dy = rand(10), rand(10)
c = sqrt.(dx .^2 .+dy .^2)
c = [c c]'
quiver(x,y,quiver=(dx,dy),line_z=repeat([c...], inner=2), c=:thermal)

```

---

<div class="post-metadata">

**Author:** ![hardy](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hardy](https://discourse.julialang.org/u/hardy)\
**Post date:** [April 19, 2021, 11:20am UTC](https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577/7 "2021-04-19T11:20:01Z")

</div>

Thanks to both of you for your help!

I have come a step further, but not quite where I want to be. Both of your code snippets produce the same output:

 ![Bildschirmfoto 2021-04-19 um 13.07.17](https://global.discourse-cdn.com/julialang/original/3X/8/c/8c1f35e583d6b23752a85addabe36a54838e6742.png)

It looks nice but the color doesn’t seem to be strictly related to the length of the arrows. I have no idea why. The ones in the middle are the shortest and therefor should be less bright than the upper ones.

Help is very welcome

---

<div class="post-metadata">

**Author:** ![hardy](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hardy](https://discourse.julialang.org/u/hardy)\
**Post date:** [April 19, 2021, 11:22am UTC](https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577/8 "2021-04-19T11:22:38Z")

</div>

PS: Is there an argument for the arrow hat size? I found one for Makie but not for Plots.gr()

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 19, 2021, 11:46am UTC](https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577/9 "2021-04-19T11:46:42Z")

</div>

In `gr()` back-end all looks good to me:

 ![gr_quiver_colors](https://global.discourse-cdn.com/julialang/original/3X/6/a/6aa01af8533dba4d2190c050742d20fb7b3c2cd3.png)

But not in `pyplot()` back-end where the arrow-heads are an issue:

 ![pyplot_quiver_colors](https://global.discourse-cdn.com/julialang/original/3X/a/3/a3caab143c8a75053c1721b154dda7cd73f9ed92.png)

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [April 19, 2021, 11:54am UTC](https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577/10 "2021-04-19T11:54:32Z")

</div>

GR seems to not support the arrow series type so they draw the arrows using the shape series type.  
If you want to customize it you could also plot other shapes using that method.  
This would explain the multiple color values.

> <https://github.com/JuliaPlots/Plots.jl/blob/0a7035979f9f03eb65823deaf8aec3b844725a7d/src/recipes.jl#L1364-L1372>

---

<div class="post-metadata">

**Author:** ![hardy](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hardy](https://discourse.julialang.org/u/hardy)\
**Post date:** [April 19, 2021, 3:51pm UTC](https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577/11 "2021-04-19T15:51:31Z")

</div>

Oh fine, thank you!

---

<div class="post-metadata">

**Author:** ![hardy](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hardy](https://discourse.julialang.org/u/hardy)\
**Post date:** [April 20, 2021, 7:01pm UTC](https://discourse.julialang.org/t/quiver-plot-plots-pyplot-with-different-colors-depending-on-the-length-of-the-arrow/59577/12 "2021-04-20T19:01:43Z")

</div>

I didn’t completely understand what’s going on with the inner= -part. But I tried to understand the code example from the link above. I did the following:

> Blockquote

# Arrow position.

x = collect(0:10) # we have 11 points, not 10 !  
y = fill(0., 10)

# Arrow direction.

u = fill(1., 10)  
v = fill(10., 10) # I think the (1,10) here was a typo

# Arrow color.

c = collect(1:11) # or 0:10, but not 1:10  
plt = quiver(x, y, quiver=(u, v), line\_z=c, c=:viridis)  
display(plt)

> Blockquote

and I get:

 ![Bildschirmfoto 2021-04-19 um 17.56.27](https://global.discourse-cdn.com/julialang/original/3X/d/9/d93eac448806da46a6473c7d7d97ba6e3ae7f4c9.png)  
I gave my example another try with as many lengths as arrows. And it worked out after all ! Now I only have to change the sombreros. But I’m already delighted. I couldn’t have done that without your help!
