# Is it possible to extract data from ContourPlot?

I am trying to plot the contours of specific eigenvalue for a big matrix which can be done numerically. Once I get the contours, is it possible to extract the data as a list for example {x,y}. here is the code where I evaluated the contours (as a function of x and y) of specific eigenvalue λ

M1 = {{0, I Sin[x] + Sin[y],
3 - Cos[x] - Cos[y], -1}, {-I Sin[x] + Sin[y], 0, -1,
3 - Cos[x] - Cos[y]}, {3 - Cos[x] - Cos[y], -1,
0, -I Sin[x] - Sin[y]}, {-1, 3 - Cos[x] - Cos[y],
I Sin[x] - Sin[y], 0}};

tc = {{0, 0, 0, 0}, {0, 0, 0, 0}, {-1, 0, 0, 0}, {0, -1, 0, 0}};

Mn[n_] :=
SparseArray[{Band[{1, 1}, {4 n, 4 n}] -> {M1},
Band[{1, 5}, {4 n, 4 n}] -> {tc},
Band[{5, 1}, {4 n, 4 n}] -> {ConjugateTranspose[tc]}}]

w = 40; En = 4 w;(*w must be>1*)

MM[x_, y_] = Mn[w];
f[\[Lambda]_?NumericQ, {x_?NumericQ, y_?NumericQ}] :=
Det[MM[x, y] - \[Lambda]*IdentityMatrix[En]]

With[{\[Lambda] = 0.4},
ContourPlot[f[\[Lambda], {x, y}] == 0, {x, -2, 2}, {y, -2, 2},MaxRecursion -> 2, PlotPoints -> 20]] Now, I would like to obtain the eigenvectors of the eigenvalue λ for all {x,y} on the contour. For this I would like to extract the the values of {x, y} from the contour and then use Eigensystem[Mn[w]] to find eigenvectors associated with each single point {x,y}. How can I do that?

## 1 Answer

Update Extract line segments

lineSegments = lines /. Line[{min_, ___, max_}] :> points[[1, min ;; max]]


Plot as individual lines

ListLinePlot@lineSegments Or combined using Show

ListLinePlot /@ lineSegments // Show[#, PlotRange -> All] & You can extract the points using

plot = With[{λ = 0.4},
ContourPlot[f[λ, {x, y}] == 0, {x, -2, 2}, {y, -2, 2}, MaxRecursion -> 2, PlotPoints -> 20]]

points = plot // Cases[#, GraphicsComplex[points_, ___] :> points, Infinity] &


Verify by plotting the points

points // ListPlot The lines drawn on the contour plot are described by

lines = Cases[plot, _Line, Infinity]


The list argument to Line contains the index of the point in points.

• Thanks @Rohit Namjoshi! Just one more thing, may you please help me to manipulate the points such that I can use ListLinePlot[points]  instead of ListPlot[points] ? – valar morghulis Apr 20 '20 at 1:25
• @HD2006 Updated to show extraction and plotting of the line segments. – Rohit Namjoshi Apr 20 '20 at 2:44
• @ Rohit Namjoshi, Thanks for the help, that is cool, is it possible to connect those segments to get each circle as one segment? for simplicity, you can set w=20. I tried to do that using Sort but could not succeed. – valar morghulis Apr 20 '20 at 11:16
• ,I also tried to use Interpolation with the obtained data to generate a function of {x,y} and then I can use ListLinePlot or generate more clean date using the new function, any idea please how this can be done? – valar morghulis Apr 20 '20 at 14:43
• @HD2006 The problem is that there can be several line segments for a single contour and that they are not necessarily connected. One thing to try is to extract the first point from a segment and find the segment with the Nearest last point. Join them to form a new segment and repeat. Use Interpolation to fill in the gaps. This might work well for the inner contours but not for the outer ones. I don't have time to try that now, maybe you can experiment with that approach. – Rohit Namjoshi Apr 20 '20 at 15:25