1
$\begingroup$

I have shown several plots by Show function:

Show[p1, p2, p3, p4, p5, p6, p7, p8, PlotRange -> All]

I need to save data of all plots in one txt format, so that they can be recognizable from each other. How can I do that?

Each plot is a 2D plot say, $f_i$ as a function of $x$.

$\endgroup$
  • 2
    $\begingroup$ How can you save even one plot in a "txt" format? Do you mean the source code that generated it? $\endgroup$ – David G. Stork Mar 6 '18 at 6:02
  • $\begingroup$ @DavidG.Stork ExportString[Plot3D[x y, {x, -1, 1}, {y, -1, 1}, PlotPoints -> 3, MaxRecursion -> 0, Mesh -> None], "Text"]? What I don't understand is the goal, "recognizable from each other." @Wat, do you mean when you read the text file, you want to be able to recognize which primitives belong to which plot? Or is another program reading the text file? Why not export a list {p1, p2, p3, p4, p5, p6, p7, p8} instead of combining them with Show? $\endgroup$ – Michael E2 May 5 '18 at 12:47
1
$\begingroup$
   f[x_] := Sin[x]
    g[x_] := Cos[x]
    h[x_] := Sin[x^2]
   p1 = Plot[f[x], {x, -2 Pi, 2 Pi}, Frame -> True, PlotRange -> All, 
    PlotStyle -> Blue, PlotLegends -> Placed[{"Sin[x]"}, Above]];
    p2 = Plot[g[x], {x, -2 Pi, 2 Pi}, PlotStyle -> Red, 
   PlotLegends -> Placed[{"Cos[x]"}, Above]];
   p3 = Plot[h[x], {x, -2 Pi, 2 Pi}, PlotStyle -> Green, 
     PlotLegends -> Placed[{"Sin[x^2]"}, Above]];
    plotz = Show[p1, p2, p3]

       Now you can export and import your  plot by given below code(by   putting        youe file address.
    multidat = Cases[First@plotz, Line[data_] :> data, -4];
    Export["C:\\Users\\g.verma\\Documents\\outputw_" <> 
   IntegerString[#2] <> ".txt", #, "Table"] &~MapIndexed~multidat;
    ddf = Table[
    Import["C:\\Users\\g.verma\\Documents\\outputw_" <> ToString[i] <> 
   ".txt", "Table"], {i, 3}];
     ListLinePlot[{ddf[[1]], ddf[[2]], ddf[[3]]}, PlotRange -> All, 
    PlotStyle -> {Blue, Red, Green}, Frame -> True, 
     PlotLegends -> Placed[{"Sin[x]", "Cos[x]", "Sin[x^2]"}, Above]]

exported image

$\endgroup$
1
$\begingroup$

Similar to Gopal Vermas solution:

p1 = ListPlot[Table[{n, n^2}, {n, 1, 10}]];
p1data = Cases[p1, Point[pts__] :> pts, Infinity];

p2 = ListLinePlot[Table[{n, 2*n}, {n, 1, 10}]];
p2data = Cases[p2, Line[pts__] :> pts, Infinity];

output = Transpose[{p1data[[1, All, 1]], p1data[[1, All, 2]], p2data[[1, All, 2]]}];

Export[StringJoin[$UserBaseDirectory, "\\output.txt"], output, "Table", 
  "FieldSeparators" -> " "] 

1. 1. 2.
2. 4. 4.
3. 9. 6.
4. 16. 8.
5. 25. 10.
6. 36. 12.
7. 49. 14.
8. 64. 16.
9. 81. 18.
10. 100. 20.
$\endgroup$

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.