Message Boards Message Boards

[✓] Extract raw data (list of points for each line) from ListContourPlot?

GROUPS:

I was wondering if it is possible to extract the raw data (the x and y coordinates of the points used to draw the contour lines) from ListContourPlot. I would like to do further analysis on the shapes of certain contour lines, and having the raw data would be extremely useful. Any suggestions?

POSTED BY: Matty Mookerjee
Answer
6 months ago

Hi Matty,

you can "explore" every object in WL using the command FullForm. Then you can see how details can be extracted. In the case of contour lines one can try this:

(* example plot using "ContourShading\[Rule]None": *)    
data = RandomReal[1, {10, 10}];
plot = ListContourPlot[data, InterpolationOrder -> 3, PlotRange -> All, ContourShading -> None]

You get a list of lines (according to the levels) like so:

lines = Normal[First@plot][[2]];

To convince yourself on what you get try:

plotRange = First@AbsoluteOptions[plot, PlotRange];
Manipulate[Graphics[lines[[;;n]], plotRange], {n, 1, Length[lines], 1}]

enter image description here

Hope that helps, regards -- Henrik

EDIT:

[source code] /. conts -> plot

POSTED BY: Henrik Schachner
Answer
6 months ago

Henrik, that is very helpful. I think that I have seen FullForm before but I didn't know that it did that, exactly. That's awesome!

I appreciate the example, but did "conts" get defined in the code that you provided. I hope that I'm not being dense, but I couldn't get your example to run (except for the first part where you generated the initial data set and contour lines). If something got left out, I'd love to see it, if you wouldn't mind sending it along.

Thanks again for the help. Much appreciated.

Take care,

Matty

POSTED BY: Matty Mookerjee
Answer
6 months ago

Ah, awkward! conts should read plot - sorry! I already edited my post above, now it should run.

And here comes another little restriction on what I have stated: The method above works only as long as the objects are not atomic. So in our case we are lucky:

AtomQ[plot]
(* Out:   False *)

A counterexample would e.g. be some mesh object:

mesh = DelaunayMesh[RandomReal[1, {50, 3}]]
AtomQ[mesh]
POSTED BY: Henrik Schachner
Answer
6 months ago

Thank you so much. That clears things up perfectly. I really appreciate the help.

Matty

POSTED BY: Matty Mookerjee
Answer
6 months ago

Group Abstract Group Abstract