8
$\begingroup$

I have the following expression from which I want to extract data for a contour plot.

a = (r^2/2 + (r BesselI[1, 2 r])/(
 2 BesselI[0, 
   2 (1 + 0.24` z + 0.2` Sin[6.2834` z])])) (1.386371389484812` + 
 0.4367320268554276` (2.289733608959785` - 
    0.7936052912509126` (1 + 0.24` z + 
       0.2` Sin[6.2834` z])^2) + (-45.44` BesselI[0, 
      2 (1 + 0.24` z + 0.2` Sin[6.2834` z])] - 
    4 (1.386371389484812` + 
       0.4367320268554276` (2.289733608959785` - 
          0.7936052912509126` (1 + 0.24` z + 
             0.2` Sin[6.2834` z])^2) + 
       0.346592847371203` (1 + 0.24` z + 
          0.2` Sin[6.2834` z])^2) (4 BesselI[1, 
         2 (1 + 0.24` z + 0.2` Sin[6.2834` z])] (1 + 0.24` z + 
          0.2` Sin[6.2834` z]) - 
       4 BesselI[0, 
         2 (1 + 0.24` z + 0.2` Sin[6.2834` z])] (1 + 0.24` z + 
          0.2` Sin[6.2834` z])^2))/(4 (BesselI[1, 
       2 (1 + 0.24` z + 0.2` Sin[6.2834` z])] - 
      4 BesselI[0, 
        2 (1 + 0.24` z + 0.2` Sin[6.2834` z])] (1 + 0.24` z + 
         0.2` Sin[6.2834` z])^2)) + 
 0.346592847371203` (1 + 0.24` z + 0.2` Sin[6.2834` z])^2);     

ContourPlot[a, {z, 0, 1.5}, {r, 3, 5.5}, 
  PlotPoints -> 180, Frame -> True, FrameStyle -> {Thickness[0.0005]}, 
  Axes -> False, FrameStyle -> {Thickness[0.0005]}, ContourShading -> False]

The output should be a text file.

$\endgroup$
3
  • $\begingroup$ Related: (19859) $\endgroup$
    – Mr.Wizard
    Apr 17, 2015 at 17:20
  • $\begingroup$ Sorry for Typing here. Actually, I asked this question on the main page but didn't get some appropriate answer for it. My question is quite related to here. I used all these methods mentioned in the answers for extracting data from my ContourPlot. but it's not working for me. I want the output in a text file. F = 0.025; kv = 0.0043; kw = 0.046; A = (F + kv); B = (F + kw); Du = 290; Dv = 61.443; Dw = 7.3; G = (F - Sqrt[F^2 - 4*F*(A^2 + B^2)])^2; j11 = (-((4*F^2*(A^2 + B^2))*G^(-1)) - F); j12 = -(2*A); j13 = -(2*B); j21 = (4*A^2*F^2)/(G); j22 = (A); j31 = (4*B^2*F^2)/G; j33 = B; C1 = (k^2 (Du + $\endgroup$
    – Albert
    Jan 18, 2019 at 4:48
  • $\begingroup$ @Albert Please don't use answers as a follow-up questions. If a specific method does not work for you please create a question, include all relevant code, possibly the most reduced example, explain what exactly are you expecting and why current result does not fit your needs. There was no export part in your example. $\endgroup$
    – Kuba
    Jan 18, 2019 at 7:31

2 Answers 2

19
$\begingroup$

Original answer

For getting points of individual lines one can use Normal:

points = Cases[Normal@plot, Line[pts_, ___] :> pts, Infinity];

(I assumed here that each Line primitive contains exactly one line, as it is true for the current ContourPlot implementation. Generally, a Line primitive can contain several lines and for this case this code should be rewritten, for example:

points = Cases[Normal@plot, Line[pts_, ___] :> Flatten[pts, Depth[pts] - 3], Infinity];

)

To convert this list of lists of points into multicolumn table one can use:

multicolumnTable = Flatten /@ Flatten[PadRight[points, Automatic, ""], {2}];

Here is how it looks:

TableForm[multicolumnTable[[;; 10, ;; 6]], 
 TableHeadings -> {None, Flatten@Array[{x@#, y@#} &, Length[points]]}]

table

Exporting the multicolumn table:

Export["table.tsv", multicolumnTable]

Extracting the values of the objective function (per request in comments)

The 2D Graphics object generated by ContourPlot does not necessarily contain information on the values of the objective function (Z-coordinate). The only source of this information are contour labels which are generated according to the ContourLabels option. With the default value ContourLabels->Automatic each contour is wrapped by Tooltip with second argument being the contour label providing information on the value of the objective function. In the current ContourPlot implementation the label is simply a number and the contour is one or more Line primitives each containing exactly one actual line. Assuming that the plot does not contain any other Tooltips, it is straightforward to extract the data:

points = Flatten[
   Cases[Normal@plot, 
    Tooltip[contour_, label_] :> 
     Cases[contour, l_Line :> ArrayPad[First@l, {{0}, {0, 1}}, label]], 
    Infinity], 1];
multicolumnTable = Flatten /@ Flatten[PadRight[points, Automatic, ""], {2}];
TableForm[multicolumnTable[[;; 10, ;; 6]], 
 TableHeadings -> {None, Flatten@Array[{x@#, y@#, z@#} &, Length[points]]}]
Export["table.tsv", multicolumnTable]

table

$\endgroup$
5
  • $\begingroup$ This is working fine. THx to you and to Nasser. $\endgroup$
    – zhk
    Sep 8, 2013 at 12:15
  • $\begingroup$ how to treat the contourplot as a 3D plot and extract the data having three columns with r, z, and a(r,z)? $\endgroup$
    – zhk
    Aug 16, 2014 at 1:44
  • $\begingroup$ @MMM What you want is generally impossible to achieve because the Graphics object generated by ContourPlot not necessarily contains information on the values of the objective function. The only place where it can be is created by ContourLabels function which is (in general) arbitrary. $\endgroup$ Aug 16, 2014 at 2:35
  • $\begingroup$ @MMM Probably it would be simpler to use Plot3D with options MeshFunctions->{#3&}, Mesh->5 and then apply the approach described in my answer for extracting the mesh lines. Everything should be the same with exception to the TableForm where instead of Array[{x@#, y@#} &, Length[points]] you should have Array[{x@#, y@#, z@#} &, Length[points]]. $\endgroup$ Aug 16, 2014 at 2:36
  • $\begingroup$ @MMM But if you insist on using ContourPlot it is also possible to extract the values of the objective function when the default setting ContourLabels->Automatic is used (at least with the current implementation of ContourPlot). I'll add a method to my answer. $\endgroup$ Aug 16, 2014 at 2:48
8
$\begingroup$

Update

If you want the coordinates, then

SetDirectory[NotebookDirectory[]];
data = Cases[plot, GraphicsComplex[pts__] :> pts, Infinity];
Export["data.txt", data[[1]]];

Dimensions[data[[1]]]
(* {55340, 2} *)
data[[1, 1 ;; 10, All]] // MatrixForm

Mathematica graphics

FullForm of the plot shows the GraphicsComplex

Mathematica graphics

and GraphicsComplex has this form

Mathematica graphics

Original answer

plot = ContourPlot[a, {z, 0, 1.5}, {r, 3, 5.5}, PlotPoints -> 180, 
   Frame -> True, Axes -> False, ContourShading -> False];
data = Cases[plot, Line[pts_] :> pts, Infinity];
SetDirectory[NotebookDirectory[]];
Export["data.txt", data];

Mathematica graphics

Mathematica graphics

$\endgroup$
9
  • $\begingroup$ Well, @Nasser thx for your response. Its seems to me that the output is not in the correct format. The output should be in two columns r and z. $\endgroup$
    – zhk
    Sep 8, 2013 at 6:28
  • $\begingroup$ You asked for contour data. The above is the contour data. There are 11 rows. One for each contour. $\endgroup$
    – Nasser
    Sep 8, 2013 at 6:30
  • $\begingroup$ You are right but I need to have the data in 2 columns format. $\endgroup$
    – zhk
    Sep 8, 2013 at 6:37
  • $\begingroup$ @MMM I do not follow you. Where did this 2 columns come from and what they represent? May be all what you needed then is a simple Table command. You have to be clear what is it you want as I am not really following you. $\endgroup$
    – Nasser
    Sep 8, 2013 at 6:41
  • 1
    $\begingroup$ Let me try to make it clear, I want the data in following format. r z (say contour 1) r z (say contour 2) r z (say contour 3) Output in two columns for each contour. Clear? $\endgroup$
    – zhk
    Sep 8, 2013 at 6:45

Your Answer

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

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