I have some larger square, Hermitian matrices, M, (dimension 20, say, much more than 4) with 2 independent variables, call them x and y.

I can plot the eigenvalues as functions of x and y with:

Plot3D[Eigenvalues[M], {x,-1,1}, {y,-1,1}]

even though I obviously have no analytic expressions for the eigenvalues as functions of x and y.

I am interested in the nature of certain contours of these functions, let's say their intersections with zero.


ContourPlot[Eigenvalues[M], {x-1,1}, {y,-1,1}]

will give me plots (e.g. https://i.sstatic.net/Q1pew.png)

and in fact, the zero shown there was from a mouse-over of the central contour (cursor not present in image).

So Mathematica is clearly able to plot the contours, even the analytic zero contour.

However, if I change to

ContourPlot[Eigenvalues[M] == 0, {x-1,1}, {y,-1,1}]

I will get errors about "Mathematica can't find all the roots of the characteristic polynomial". This simple change of commands has apparently really altered how Mathematica is going about evaluating it and now it is trying (and of course failing) to solve an order-20 polynomial for analytic roots.

But from the regular contour plot, I can see that it should be capable of giving me the numerical zero contour.

So how can I get Mathematica to weaken the equality condition and give me the numerical zero contour approximately?


Perhaps there is another way altogether to extract the single contour I need from the 3D plot or the full ContourPlot?

  • $\begingroup$ See this mathematica.stackexchange.com/questions/20281/…. I have used it and works. $\endgroup$
    – carlosayam
    Commented Jan 6, 2014 at 22:02
  • $\begingroup$ Forgot to say that there is the caveat that the orientation wasn't uniform (some lines from left to right, others from right to left - kind of, you get the point) and I fixed that by hand based on my particular case. $\endgroup$
    – carlosayam
    Commented Jan 6, 2014 at 22:08
  • $\begingroup$ Related: (23363), (32681), (32734) $\endgroup$
    – Mr.Wizard
    Commented Jan 6, 2014 at 23:18
  • 2
    $\begingroup$ Please provide a "representative" example matrix. $\endgroup$ Commented Jan 7, 2014 at 3:36

2 Answers 2


In such cases, you can plot just the contour that you want (i.e. without == 0) and remove the colored polygons in a post-processing step:

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, MaxRecursion -> 3,
    Contours -> {0}, ContourStyle -> Black, Mesh -> None] /. _Polygon -> Sequence[]

enter image description here

  • $\begingroup$ Based on the three questions I linked as related above, do you feel that this question is sufficiently different to justify not being closed as a duplicate? $\endgroup$
    – Mr.Wizard
    Commented Jan 6, 2014 at 23:19
  • $\begingroup$ @Mr.Wizard Without OP's actual matrix it's hard to say, but perhaps a little different: In all the other three questions, the issue is some variant of "function only touches 0 but doesn't cross it". I don't think that's the problem here. According to OP, their function just won't evaluate (or has trouble evaluating) with an == 0 sign, but it's OK without it. Given this scenario, they want to be able to plot a single contour without using == 0. I'm not entirely sure if their problem is solved by the other answers, but I certainly won't complain if this is closed as a duplicate :) $\endgroup$
    – rm -rf
    Commented Jan 7, 2014 at 0:00
  • $\begingroup$ Okay. +1 for a nice concise solution to an apparently different problem. :-) $\endgroup$
    – Mr.Wizard
    Commented Jan 7, 2014 at 2:01
  • $\begingroup$ As an alternative to /. _Polygon -> Sequence[] you could use the Option ColorFunction -> (None &). $\endgroup$
    – Mr.Wizard
    Commented Jan 7, 2014 at 2:03
  • $\begingroup$ I didn't post the actual matrix only because it's such a mess and I think the problem is more general. It seems that without the ==0 addition, mathematica uses specific values of the parameters x and y and then finds the eigenvalues (real numbers) and plots them. No problem. But with the addition of the ==0, it seems that it tries to evaluate the plot argument functions (Eigenvalues[M]) first, so of course we get 20 Root[] type answers that can't be found analytically, and it gives errors. $\endgroup$
    – Steve
    Commented Jan 7, 2014 at 17:49

rm -rf provided a solution to the problem which works. Thanks very much to him.

Sort[Eigenvalues[M]][[s]], {x, -1,1}, {y, -1,1}, Contours -> {w},
PlotRange -> {{-1, 1}, {-1,1}}] /. _Polygon -> Sequence[]
, {s, 1, Dimensions[M][[1]]}

Will plot the contours of all the eiegenvalues of M with height w (with w=0 we get the special case I asked for here)


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.