0
$\begingroup$

I'm trying to extract the data from a parametric plot via the following:

Subscript[\[Epsilon], e] = 0.0005;
Subscript[R, 0] = 15;
f[\[Theta]_, \[Phi]_] := 
  1 + (4 Subscript[\[Epsilon], e])/(1 - 
       3 Subscript[\[Epsilon], e]) (Cos[\[Theta]]^4 + 
      Sin[\[Theta]]^4 (1 - 2 Sin[\[Phi]]^2 Cos[\[Phi]]^2));
x = Subscript[R, 
    0] (f[\[Theta], \[Phi]] Sin[\[Theta]] Cos[\[Phi]] + 
     D[f[\[Theta], \[Phi]], \[Theta]] Cos[\[Theta]] Cos[\[Phi]] - (D[
         f[\[Theta], \[Phi]], \[Phi]] Sin[\[Phi]])/Sin[\[Theta]]);
y = Subscript[R, 
    0] (f[\[Theta], \[Phi]] Sin[\[Theta]] Sin[\[Phi]] + 
     D[f[\[Theta], \[Phi]], \[Theta]] Cos[\[Theta]] Sin[\[Phi]] + (D[
         f[\[Theta], \[Phi]], \[Phi]] Cos[\[Phi]])/Sin[\[Theta]]);
z = Subscript[R, 
    0] (f[\[Theta], \[Phi]] Cos[\[Theta]] - 
     D[f[\[Theta], \[Phi]], \[Theta]] Sin[\[Theta]]);

Now, Parametric2D gives me the desired figure.

ParametricPlot[{x, y}, {\[Theta], 0, 2 \[Pi]}, {\[Phi], 0, 
      2 \[Pi]}, PlotStyle -> None, MeshFunctions -> {#3 &}, 
     Mesh -> {{{\[Pi]/2, Directive[Opacity[1], Blue, Thick]}}}]

Next I need to save the data, and I do the following

data = Cases[
    ParametricPlot[{x, y}, {\[Theta], 0, 2 \[Pi]}, {\[Phi], 0, 
      2 \[Pi]}, PlotStyle -> None, MeshFunctions -> {#3 &}, 
     Mesh -> {{{\[Pi]/2, Directive[Opacity[1], Blue, Thick]}}}], 
    Line[data_] :> data, -4, 1][[1]];
SetDirectory[NotebookDirectory[]];
Export["Groov_pro_Diff_THAf.txt", data, "Table"]

Through the above code, although I generate a txt file, I get the error

Part1 of {} does not exist

I have no issues with the plot, ParametricPlot[...] command gives me the correct figure, but not during saving the data. Here I would like to save the data as two columns only. Can anyone suggest how to extract the data in such a case?

$\endgroup$
0
1
$\begingroup$

For such situations Reap and Sow are quite useful:

data = Reap[ParametricPlot[{x, y}, {\[Theta], 0, 2 \[Pi]}, {\[Phi], 0, 2 \[Pi]}, PlotStyle -> None, MeshFunctions -> {#3 &}, Mesh -> {{{\[Pi]/2, Directive[Opacity[1], Blue, Thick]}}}, EvaluationMonitor :> Sow[{\[Theta], \[Phi], x, y}]]][[2]]

This returns list {\[Theta], \[Phi], x, y}. If you want to save only {x,y} use it inside Sow instead.

$\endgroup$
7
  • $\begingroup$ Thanks, I would like to save in 4 columns rather than {[Theta], [Phi], x, y} format. I'm do use "Table" while exporting, but it doesnt give the desired result. $\endgroup$
    – newstudent
    Apr 28 '21 at 13:07
  • 1
    $\begingroup$ That is you want only list of {{x_1,y_1},...,{x_N,y_N}} ? If so, using Sow[{x, y}] should work. For exporting indeed either export data[[1]] or in the definition of data use [[2,1]] at the end. $\endgroup$
    – Andrzej
    Apr 28 '21 at 13:16
  • $\begingroup$ Thanks, Using Parametric2D I have encountered problem I now need to plot only the circumference points rather than all the center regions. $\endgroup$
    – newstudent
    Apr 28 '21 at 13:23
  • 1
    $\begingroup$ My answer was really about how to extract data from a plot. If you want to extract only some specific points, then this will depend on what exactly is your goal (I assume it is bit more than this example, as otherwise you'd probably just generate a table with points, rather than going through plot). Anyway, in this example you could use If[x^2 + y^2 >= 15^2, Sow[{x, y}]] $\endgroup$
    – Andrzej
    Apr 28 '21 at 13:49
  • 1
    $\begingroup$ Yes, this is what I meant: data = Reap[ParametricPlot[{x, y}, {\[Theta], 0, 2 \[Pi]}, {\[Phi], 0, 2 \[Pi]}, EvaluationMonitor :> If[x^2 + y^2 >= 15^2, Sow[{x, y}]]]][[2, 1]] $\endgroup$
    – Andrzej
    Apr 28 '21 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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