So I have a code which, after a very long computing time, spits out a single, very non-trivial plot in one variable. Repeating the computation with different parameter values, I output a different plot. I would really like to combine a handful of these into a single plot on one set of axes. In other words, I'd like to run my crazy script, add the curve to the plot, run the code again and add the resulting curve to the plot.

As a much over-simplified example, imagine I had

f[n_, x_] := Sin[n*x]; A = Plot[f[1, x], {x, 0, Pi}] B = Plot[f[2, x], {x, 0, Pi}] BB = Plot[f[3, x], {x, 0, Pi}]

and I wanted to plot A, B, and BB on the same plot over the same range. Now obviously, in this example this is painfully easy to do, but imagine that each of those plots resulted from running a long, highly non-trivial script, and to generate the next plot, I had to run the same script again with different parameters.

Is there an easy way to sort of iteratively save these plots and get them into one plot with one set of axes?

  • 2
    $\begingroup$ Have you seen Show? $\endgroup$ – Edmund Jan 22 '17 at 1:11
  • $\begingroup$ does each plot need a lot of memory ? How many plots are there ? $\endgroup$ – andre314 Jan 22 '17 at 1:11
  • $\begingroup$ I need to plot about 5-6 functions of one variable on the same set of axes; and no, it's not a ton of memory, just a long time-consuming script. I have indeed heard of Show, but isn't that only able to combine figures into something like 2x2 blocks in one figure? $\endgroup$ – Benighted Jan 22 '17 at 1:21
  • $\begingroup$ Are there any examples of Show producing a 2x2 block of plots in the docs for Show? $\endgroup$ – Michael E2 Jan 22 '17 at 1:26
  • $\begingroup$ Add some code using examples like Plot[Sin[x],{x,0,1}],Plot[Cos[x],{x,0,1}], and so on to demonstrate what you want as the output with these multiple plots. You need to demonstrate what you mean by "combine" has this has more than one meaning. $\endgroup$ – Edmund Jan 22 '17 at 1:31

There are plenty of solutions, for example :

someParameter= 3
crazyCode[input_]:=Plot[someParameter Sin[input x],{x,0, 2 Pi}];
resultingPlotList=Association[] (* intialisation of the results *)
AppendTo[resultingPlotList,myInputValues[someParameter,#] -> crazyCode[#]]& /@ inputValues;   
(* : or any kind of iteration you have *)
resultingPlotList //Values //Show  

enter image description here

enter image description here

You only loose your work in the case of a kernel crash.

| improve this answer | |
  • $\begingroup$ Thanks a lot for this. The problem is that each iteration in which I run my code to produce a plot, there's stages where I have to do some fiddling and manually find roots and such, so it's not totally automated. I'm sorry I should have pointed that out in my questions. But I appreciate this, thanks a lot. $\endgroup$ – Benighted Jan 22 '17 at 2:43
  • $\begingroup$ Just save your plot using Export[filename,plot] in each iteration, then load each file into a different variable and use Show to plot them all in the same graph. $\endgroup$ – Felix Jan 22 '17 at 4:07

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.