# Plotting complex eigenvalues of a matrix as a function of a parameter: developing a color function to represent imaginary part

Consider the example below:

Z0 = {{1, 0, 0}, {0, 1, -2}, {3, 0, 1}};
Z1 = {{1, 2, 0}, {1, 0, 3}, {0, 0, 0}};
Z2 = {{0, 0, 0}, {-2, 0, 1}, {0, -3, 1}};

Plot[
Sort@(Re /@ Eigenvalues[Z0 + u Z1 + u^2 Z2]),
{u, -2, 2},
PlotRange -> All,
(*ColorFunction ->,*)
AspectRatio -> 1
]


which produces the following output: Basically, I am plotting the real part of the eigenvalues of the matrix $Z(u) = Z_0 + u Z_1 + u^2 Z_2$ as a function of the parameter $u \in [-2,+2]$. The use of Sort ensures that the curves are "joined correctly", avoiding the following undesired effect (at $u=1$ in this example): Is it possible to develop a ColorFunction for this plot, so that the color of each point is a function of the respective imaginary part of the eigenvalue (which will allow to identify which curve represents each mode)?

Here's a first pass at an answer. It's not perfect, because it's hard to control the color scheme numerically. It could be improved easily, I think, but we'd require more information about what you'd need. In any case...

Z0 = {{1, 0, 0}, {0, 1, -2}, {3, 0, 1}};
Z1 = {{1, 2, 0}, {1, 0, 3}, {0, 0, 0}};
Z2 = {{0, 0, 0}, {-2, 0, 1}, {0, -3, 1}};
f[u_] = Eigenvalues[Z0 + u Z1 + u^2 Z2];


We first define a function which does the Sorting for us on the fly. This allows us to plot the different branches separately:

g[k_Integer /; 1 <= k <= 3][u_] := SortBy[f[u], {N@*Re, N@*Im}][[k]]


Then, we use plot the real part of this and use the imaginary part as the input to a ColorFunction:

Show@Table[Plot[Re@g[k][u], {u, -2, 2},
PlotRange -> All,
ColorFunction -> Function[{x, y}, Hue@Im@g[k][x]],
ColorFunctionScaling -> False,
AspectRatio -> 1
], {k, 1, 3}] • Thank you so much. That was exctly what I needed. Just a short correction: the ColorFunction in this case should be: ColorFunction -> Function[{x, y}, Hue@Im@g[k][x]] instead of ColorFunction -> Function[{x, y}, Hue@Im@g[x]] – Renato Orsino Mar 23 '17 at 1:40
• @RenatoOrsino. Fixed! – march Mar 23 '17 at 1:42