This question has multiple parts to it. The setup is that I have a matrix that is a function of two parameters a and b. I wish to plot the eigenvalues of this matrix along a general path in the a-b plane and I want these two branches to have the correct coloring. For example, for the simple path for {a,b} from {0,0} to {1,0} the following works:

testMat[a_, b_] := {{2 a, 3 b^2}, {2 b, 4 a}};     
Plot[Evaluate@Eigenvalues[testMat[t, 0]], {t, 0, 1}]

At this point please note that Evaluate must be included in the second line for the two branches to have different colors. My first question is thus

  1. Why is Evaluate necessary to get the correct colors for the eigenvalue plots?

Now suppose that I wish to plot the eigenvalues on a path that goes from {0,0} to {0,1} to {1,1}. I implemented this in the following way

testfunc[t_] = Evaluate@Piecewise[{{testMat[t, 0], 0 <= t <= 1}, {testMat[1, t - 1],
  1 < t < 1.5}}, {0, 0}];
Plot[Eigenvalues@testfunc[t], {t, 0, 1.5}]

However as you can see the two branches have the same color. Somehow Mathematica does not understand that they are two separate plots. Thus my final two questions are:

  1. How do I get the two branches colored separately?

  2. Is there a better way of plotting along a path that Mathematica will find more agreeable?

Thanks in advance!

EDIT: All answers are great but there is a significant problem using First/Last or dot products since the Eigenvalues are listed from largest to smallest while when plotting we are interested in smooth functions (i.e the largest eigenvalue will always be blue and the smaller orange). For example:

testMat2[a_, b_] := {{2 a, 3 b^2}, {2 b, 4 a^2}}; 
Plot[{First@Evaluate@Eigenvalues[testMat2[t, 0]], 
      Last@Evaluate@Eigenvalues[testMat2[t, 0]]}, {t, 0, 1.6}]

What do I do to fix this?

Edit 2: Inspired by the excellent answers below, the easiest method for me was to create a Table of eigenvalues and then use Interpolation to create a vector values function. Now Plot[{interpFunc[t][[1]], interpFunc[t][[2]],...}, {t,0,1}] works beautifully!


3 Answers 3


1) Basically, Mathematica has no way of knowing whether to treat the two curves as having the same or distinct colors. Using Evaluate tells it to use distinct colors. (The underlying reasons relate to the order of evaluation.)

2) Evaluate has no effect for testfunc, because it cannot decide which part of Piecewise to use until t is provided. Replacing Set by SetDelayed does not help. Instead try

 Plot[{First@Eigenvalues@testfunc[t], Last@Eigenvalues@testfunc[t]}, {t, 0, 1.5}]

3) Probably not, but I am not sure.


The problem is that at the time of the call to Plot, it is not clear that it is about two function that are to plot. Actually, you tell Mathematica's Plot command to plot an $\mathbb{R}^2$-valued function. You can circumvent this issue, e.g. with ListLinePlot:

f[t_] := Eigenvalues[Piecewise[{{testMat[t, 0], 0 <= t <= 1}, {testMat[1, t - 1], 1 < t <= 1.5}}, {0, 0}]];
tlist = Subdivide[0., 1.5, 250];
ListLinePlot[Transpose[{tlist, #}] & /@ Transpose[testfunc /@ tlist]]

I'll add a couple of lines of code without using First, Last

testMat[a_, b_] := {{2 a, 3 b^2}, {2 b, 4 a}};
Plot[{Eigenvalues[testMat[t, 0]].{1, 0}, 
  Eigenvalues[testMat[t, 0]].{0, 1}}, {t, 0, 1}, 
 PlotStyle -> {Green, Red}]

testfunc[t_] = 
  Piecewise[{{testMat[t, 0], 0 <= t <= 1}, {testMat[1, t - 1], 
     1 < t < 1.5}}, {0, 0}];
Plot[{Eigenvalues@testfunc[t].{1, 0}, 
  Eigenvalues@testfunc[t].{0, 1}}, {t, 0, 1.5}]



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.