Plotting results from for loop

In summary, when plotting results from a "for" loop in Mathematica, it is best to not create any plots inside the loop and instead collect the points and then create the plot after the loop is finished. This can be done using the Table function or by using Sow and Reap to save up results. There are multiple ways of achieving this in Mathematica, but it is important to avoid creating plots inside the loop for efficiency.
  • #1
nikolafmf
114
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
  • #2
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.
 
  • #3
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
 

Related to Plotting results from for loop

1. What is a for loop in programming?

A for loop is a control structure in programming that allows a set of instructions to be repeated for a given number of times or until a certain condition is met.

2. How do I plot results from a for loop in a graph?

To plot results from a for loop in a graph, you will need to save the results of each iteration in a variable and then use a plotting library or tool to create a graph using those variables. The specific steps may vary depending on the programming language you are using.

3. Can I plot different results from multiple for loops in one graph?

Yes, it is possible to plot results from multiple for loops in one graph. You will need to use a plotting library or tool that allows for multiple data sets to be plotted on the same graph and ensure that the variables from each for loop are properly organized and labeled.

4. How can I customize the appearance of the plotted graph?

The customization options for a plotted graph will depend on the plotting library or tool you are using. However, most libraries and tools offer options to change the color, size, and style of the plotted lines or points, add labels and titles, and adjust the axes and gridlines.

5. Is it possible to save the plotted graph as an image or file?

Yes, most plotting libraries and tools allow you to save the plotted graph as an image or file. The specific steps may vary, but it is typically done by using a built-in function or by exporting the graph in a specific format, such as PNG or PDF.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
358
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
896
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top