Plotting results from for loop

Click For Summary
To plot results from a "for" loop in Mathematica, it's recommended to collect all calculated points first and then create a single plot rather than plotting each point individually within the loop. Using the `Table` function is a more efficient alternative to `For`, as it gathers results into a list that can be plotted all at once. For example, `ListPlot[Table[RandomReal[{-1, 1}, 2], {6}], Joined -> True]` demonstrates this approach. If a "for" loop must be used, the `Sow` and `Reap` functions can be utilized to accumulate results, as shown in the example `ListPlot[Reap[For[i=1, i<=6, i++, Sow[RandomReal[{-1,1}, 2]]]][[2,1]], Joined -> True]`.
nikolafmf
Messages
112
Reaction score
0
Plotting results from "for" loop

Hello,

I have a problem which it seems I couldn't solve. I have "for" loop which gives results of some calculations as, say, vectors with two coordinates. What I want is to plot the result as a point in the xy plane. This loop goes on and on with the calculations from the beginning to the end for some 1000 times. So I want to get the results, 1000 points, plotted on only one graph, not each point on its own graph. Ideally they could be joined by a line.

How can I do this? I work in Mathematica. Nikola
 
Last edited:
Physics news on Phys.org
I don't know any Mathematica, but in any other program, you would NOT create any plots inside the loop...you simply collect the points and THEN right after the loop, you create the one plot of interest.

Unless you are plotting real time data coming into the computer, etc., there is no need to even be updating a plot after every iteration of a loop...the loop will be done soon enough.
 
Users coming from other programming languages usually pounce on For. In Mathematica there are usually other, often better, ways of doing this. Here is one way.

Code:
ListPlot[Table[RandomReal[{-1, 1}, 2], {6}], Joined->True]

Table is almost like For, it will repeat a calculation a certain number of times. The difference is that it will package up the results of all those calculations into a list and return it to you, so you can ListPlot the result for example.

You can replace that RandomReal with other calculations. You can even do multiple steps if you carefully use a semicolon at the end of each of those and you make the last item be the result that you want to have stored in the list.

Code:
ListPlot[Table[x=RandomReal[{-1,1}];y=RandomReal[{-2,2}];x=2*x-y;{x,y},{6}],Joined->True]

But if you are compelled to use a For then you can use Sow and Reap to save up results.

Code:
ListPlot[Reap[For[i=1, i<=6, i++, Sow[RandomReal[{-1,1}, 2]]]][[2,1]],Joined->True]

In Mathematica there are always a dozen different ways of doing anything, at least several of which are completely incomprehensible. You could, for example, create an empty list and inside the For append the latest value to the end of the list AND then save that as the new value of the list. And there are other things you could do, but hopefully this is enough to get you started
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K