# Julia: Gradation Plotting

**URL:** <https://discourse.julialang.org/t/julia-gradation-plotting/65683>\
**Category:** General Usage\
**Tags:** plotting, plots\
**Created:** [August 2, 2021, 2:17pm UTC](https://discourse.julialang.org/t/julia-gradation-plotting/65683 "2021-08-02T14:17:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yqqwe123](https://avatars.discourse-cdn.com/v4/letter/y/13edae/32.png) [@yqqwe123](https://discourse.julialang.org/u/yqqwe123)\
**Post date:** [August 2, 2021, 2:17pm UTC](https://discourse.julialang.org/t/julia-gradation-plotting/65683/1 "2021-08-02T14:17:55Z")

</div>

I try to plot (ReE,ImE) graph, here E = Re[E] + iIm[E] are the complex eigenvalues of a matrix H.

However, in this code, the plotting is colored by some default color.  
I want to plot each (ReE,ImE) by coloring with blue gradation according to the value of the norm of eigenvector of E.  
What should I do?

My code is below:

```julia
eigenvalue,eigenvector = eigen(H)
plot(real.(eigenvalue),imag.(eigenvalue),st = scatter,markersize = 0.2)

```

thank you 😶

---

<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:** [August 2, 2021, 2:51pm UTC](https://discourse.julialang.org/t/julia-gradation-plotting/65683/2 "2021-08-02T14:51:41Z")

</div>

This is not exactly what is requested but may be useful:

```julia
using LinearAlgebra, Plots; gr()
H = rand(-10:10,40,40)
eigenvalue,eigenvector = eigen(H)
cgrad1 = cgrad(:blues,length(eigenvalue), categorical=true)
ix = sortperm(abs.(eigenvalue))
scatter(eigenvalue[ix], ratio=1, mc=cgrad1[:], ms=5,msw=0,label=false)

```

![Plots_gr_scatter_colors_by_sorted_magnitude](https://global.discourse-cdn.com/julialang/original/3X/c/4/c4d64b7c3e9eb7a14597f36a5b668f5522fdab49.png)

_ **NB:** the colors are mapped to the sorted magnitudes and not proportionally to the magnitudes themselves…_

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [August 2, 2021, 3:43pm UTC](https://discourse.julialang.org/t/julia-gradation-plotting/65683/3 "2021-08-02T15:43:19Z")

</div>

Here’s a solution using AlgebraOfGraphics:

```julia
using LinearAlgebra
using CairoMakie
using AlgebraOfGraphics

H = rand(-10:10, 40, 40)
λ, v = eigen(H)

draw(mapping([real(λ)] => "Re", [imag(λ)] => "Im", color=[abs.(λ)] => "Magnitude") *
     visual(colormap=:blues),
     axis=(width=300, height=200))

```

![image](https://global.discourse-cdn.com/julialang/original/3X/b/5/b50c69cc2e938c3c681d6bc1912e63e39ab0e92c.png)

This is using the magnitude of the eigenvalues (the eigenvectors returned by `eigen` all have norm 1, and for every eigenvector \vec{v} and k\neq 0 the vector k\vec{v} is also an eigenvector so I’m not sure why you would want to plot the eigenvector norms?).

Edit: and with simple Makie:

```julia
using LinearAlgebra, CairoMakie

H = rand(-10:10, 40, 40)
λ, v = eigen(H)

scatter(real(λ), imag(λ), color=abs.(λ), colormap=:blues)

```

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [August 2, 2021, 3:58pm UTC](https://discourse.julialang.org/t/julia-gradation-plotting/65683/4 "2021-08-02T15:58:30Z")

</div>

In `Plots`, use `marker_z` (or `mz`) to specify the value to map to marker color.

```julia
eigenvalue,eigenvector = eigen(H)
scatter(eigenvalue, marker_z = abs.(eigenvalue), 
        markercolor = cgrad(["white", "blue"]), 
        legend=false, cbar=true)

```

![tmp](https://global.discourse-cdn.com/julialang/original/3X/f/6/f6a9000aae99678f86677b63780e78f5ebd3f85c.png)

---

<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:** [August 2, 2021, 4:05pm UTC](https://discourse.julialang.org/t/julia-gradation-plotting/65683/5 "2021-08-02T16:05:01Z")

</div>

@yha, may use argument: `color =:blues`

---

<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:** [August 2, 2021, 4:20pm UTC](https://discourse.julialang.org/t/julia-gradation-plotting/65683/6 "2021-08-02T16:20:34Z")

</div>

> [@sijo](#):
>
> so I’m not sure why you would want to plot the eigenvector norms?

Note that `H` was made up and not by the OP.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [August 2, 2021, 5:33pm UTC](https://discourse.julialang.org/t/julia-gradation-plotting/65683/7 "2021-08-02T17:33:13Z")

</div>

But the same is true of any matrix: although not documented I think, `eigen` always returns normalized eigenvectors.

Well there’s probably a matrix type out there that specializes `eigen` and returns non-normalized vectors, but even so it seems like a detail of the implementation with no mathematical meaning…

---

<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:** [August 2, 2021, 5:39pm UTC](https://discourse.julialang.org/t/julia-gradation-plotting/65683/8 "2021-08-02T17:39:11Z")

</div>

You are right, it is the eigenvalues that give magnitude along each eigen direction.
