0
$\begingroup$

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][[1]]; totaltime[i] = temp[i][[2]]; id[i] = temp[i][[3]]; edge[i] = temp[i][[4]]; area[i] = temp[i][[5]], {i, a, b}];

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

Do[j = Flatten[Position[id[a], id[b][[i]]]][[1]]; 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.

$\endgroup$
5
  • 2
    $\begingroup$ 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. $\endgroup$
    – Kuba
    Aug 26, 2013 at 6:14
  • 1
    $\begingroup$ 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. $\endgroup$
    – Yves Klett
    Aug 26, 2013 at 6:36
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Aug 26, 2013 at 7:49
  • $\begingroup$ 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. $\endgroup$
    – Lithium27
    Aug 26, 2013 at 8:23
  • $\begingroup$ The code you posted has several syntax errors. Could you post an updated version without such errors? $\endgroup$
    – m_goldberg
    Aug 26, 2013 at 16:02

3 Answers 3

4
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$
    – Lithium27
    Aug 27, 2013 at 5:58
  • $\begingroup$ Depending on your speed needs, Table should work just fine, but if you need to optimize things, look at Array, also. $\endgroup$
    – rcollyer
    Aug 27, 2013 at 12:16
  • $\begingroup$ thank you for your help.Using Table works. $\endgroup$
    – Lithium27
    Aug 28, 2013 at 8:26
6
$\begingroup$

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]

enter image description here

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.

$\endgroup$
1
  • $\begingroup$ I get your answer but after "Do" construction, it only gives the last result and not all the data. $\endgroup$
    – Lithium27
    Aug 27, 2013 at 6:16
5
$\begingroup$

You can use Sow and Reap

r = First@Last@Reap@Do[
      Sow[Table[{i, RandomReal[1]}, {i, 10}]];
      Print[" iam working"];
      Sow[Table[{i, RandomReal[1]}, {i, 10}]];
      Print[" iam working"];
      Sow[Table[{i, RandomReal[1]}, {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]

Mathematica graphics

$\endgroup$
1
  • 1
    $\begingroup$ Mesh -> All is neater, as it puts the mesh points on the data points. $\endgroup$ Aug 26, 2013 at 15:05

Your Answer

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

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