# How do I show the combined plot from the do-loop?

I have a do-loop to do the calculations from a set of data and plot the graphs with different results, that was obtained from the calculations. I was able to get multiple graphs but I want to ListPlot the different graphs in one plot. Basically, I just want one graph by the end of do-loop plotting the results.

a = 0; b = 1; Do[a++; b++;t[a]; t[b];

Do[temp[i] = Transpose[t[i]]; counter[i] = temp[i][]; totaltime[i] = temp[i][]; id[i] = temp[i][]; edge[i] = temp[i][]; area[i] = temp[i][], {i, a, b}];

VT = Table[Null, {i, 1, z}]; (*VT={reduced area with time}*)

Do[j = Flatten[Position[id[a], id[b][[i]]]][]; If[edge[b][[i]] == edge[a][[j]], V = (area[b][[i]] - area[a][[j]])/scale; VT[[i]] = {totaltime[a][[j]], V}], {i, 1, z}];
minE = Min[edge[b]]; maxE = Max[edge[b]];

Table[T[i] = Take[VT], {i, 1, m - 1, 1}]; Print[T[i]] Print[ListPlot[{T[i]}, Frame -> True, PlotRange -> {minE - 6 - 1, maxE - 6 + 1}, Axes -> False, GridLines -> {None, Table[i, {i, minE - 6, maxE - 6}]}, PlotLabel -> StringJoin["Interval = ", ToString[interval]]]] , {i, 1, m - 1, 1}]

I have edited the code. I'm new to this so please forgive me for the mistakes. I know that ListPlot can plot multiple sets in one plot like"ListPlot[{data1,data2,...}, Frame -> True," However I have like hundreds of output data and that's where I can't seems to do. My code above gives multiple plots and I just want one output plot.

• ListPlot can handle multiple data sets in form {data1, data2, ...}. Also, you can take a look at Show. Or I'm missing something but it's hard to tell since there is not much information in your question. – Kuba Aug 26 '13 at 6:14
• Welcome to Mathematica.SE! Please include code (best as minimum working example) and show us what you already tried, otherwise this Q&A will only be of very limited use for future visitors and might be closed. – Yves Klett Aug 26 '13 at 6:36
• Lithium, I assumed the anonymous edit to this post was yours and approved it, with added formatting. I'm afraid it's still not clear what you want, at least to me. – Mr.Wizard Aug 26 '13 at 7:49
• Sorry, for not being clearer. I want to plot this 13 points at different time, Time Vs V. So I want to do this calculation 10 times and get 10 sets of Time Vs V. Then combine all in one graph. So I would have 10 set of V at 10 different times. The graph would show change of V with change of time. I'm trying my best to be clear. I hope that I'm . Thanks for helping. – Lithium27 Aug 26 '13 at 8:23
• The code you posted has several syntax errors. Could you post an updated version without such errors? – m_goldberg Aug 26 '13 at 16:02

This is to long for a comment, and not a proper answer.

The primary disadvantage of using Do, For, and While is they do not produce a return value. So, by using them, the user is forced into a c/c++ mode of thought where intermediate values are output to stdout (which is what Print effectively does), instead of using a form that produces results allowing further calculations to make use of them. The output Print does produce is usable in the same manner that the output of printf is. There are legitimate uses for these functions, like speed and compilability, but those should only be considered if the more functional forms like Table and Map fail. In fact, you use Table, but only to create DownValues for T which takes a speed hit.

• I realized I could do without the table and still get the plots. Now I'm only working with a few hundreds data but I will have to do bigger scale and I don't want a lot of plots. If I don't use do-loop is there any other way? – Lithium27 Aug 27 '13 at 5:58
• Depending on your speed needs, Table should work just fine, but if you need to optimize things, look at Array, also. – rcollyer Aug 27 '13 at 12:16
• thank you for your help.Using Table works. – Lithium27 Aug 28 '13 at 8:26

What makes your question hard to answer is that you have boxed yourself in by writing the code in such an awkward fashion -- hence you see an answer like Nasser's which has all those Reaps and Sows. Most likely, none of that is necessary. Let's take the heart of your question (which appears to be): "I want to do this calculation 10 times and get 10 sets of Time Vs V. The graph would show change of V with change of time."

What you should do (as hinted at by Kuba) is to make all the calculations (giving you 10 times 10 sets of data), give this a name, and then plot it at the end. For example, say your calculations give data

data = RandomVariate[NormalDistribution[], {10, 13}];


Of course this is just a surrogate for your calculated data (I can't understand the code you posted becaue it does not function, e.g., Transpose[...,{i,a,b}] is not a legal command). Now you can plot these 10 different data sets as a function of time:

ListLinePlot[data] If you do end up using the Do construction (and there are almost surely better ways to do this as well), at least pull the Plot commands from outside the main loop.

• I get your answer but after "Do" construction, it only gives the last result and not all the data. – Lithium27 Aug 27 '13 at 6:16

You can use Sow and Reap

r = First@Last@Reap@Do[
Sow[Table[{i, RandomReal}, {i, 10}]];
Print[" iam working"];
Sow[Table[{i, RandomReal}, {i, 10}]];
Print[" iam working"];
Sow[Table[{i, RandomReal}, {i, 10}]]
, {2}];

Dimensions[r]
(* {6, 10, 2} *)


so there are 6 plots there, each has 10 rows and 2 columns

ListLinePlot[r, Joined -> True, Mesh -> True, MeshStyle -> Red] • Mesh -> All is neater, as it puts the mesh points on the data points. – Simon Woods Aug 26 '13 at 15:05