Message Boards Message Boards

5 Replies
2 Total Likes
View groups...
Share this post:

Displaying Graphics Objects in Loops - How?

Posted 10 years ago

this is a very simple question ... hopefully someone has a quick answer.

suppose I define a simple graphics object and display it ... for example ...

obj = Line[ { {-3, -1}, {3, -1} } ] ;      Graphics[obj]

these 2 lines of code work fine - and I get a Line in Mathematica.

Now ... I want to do the same thing inside a loop, such as a For Loop. But Mathematica gives no output. WHY?? Even if I add a Show command outside the Graphics statement, this still does not work.

For [ j = 1,  j < 3,  j++,   Graphics[obj]

thanks, Pete

POSTED BY: pete p
5 Replies
Posted 10 years ago

You can use the command Table as following

obj = Line[{{-3, -1}, {3, -1}}];
Table[Graphics[obj], {i, 1, 3}]

I think you want to do something like this

Table[Graphics[Line[{{-3, -1}, {i, -1}}], Axes -> True], {i, 1, 3}]

I hope you've given any idea to solve your problem

POSTED BY: Luis Ledesma
Posted 10 years ago

thanks for your reply!

I didn't do a very good job of explaining my exact problem. It is not possible for me to eliminate the FOR Loop because there are other statements inside that loop. For simplicity, I did not show them. But the FOR Loop is running a simulation, and each time the loop completes an iteration - I would like to show the Graphics.

I did discover that if I put a Print statement around the Graphics, Mathematica will show the results Print[ Graphics[obj] ] The only problem with the Print statement is that it looks as if part of the Graphics is cut off ... Mathematica is not displaying the full region defined by the graphics. Perhaps there is some sort of option with Print that tells the software to expand the boundaries so that all graphics objects will be displayed?

BTW, I know you are wondering why I do a simulation with a FOR Loop. The answer is ... for very large and complicated simulations, I have found it helpful to control the main iteration (over time) with a simple FOR Loop. It actually makes the calculations much easier to de-bug when you are trying to work out what happened at any specific place in a given iteration. :-)

thanks, Pete

POSTED BY: pete p

Rather than use Print, put the Graphics into a TextCell and use CellPrint as in this example:

For[i = 0, i < 5, i++, 
   TextCell[Graphics[{Line[{{-3, -1}, {3, -1 + i}}]}], "Text"]]] ;
POSTED BY: David Reiss
Posted 10 years ago

awesome ... that reply is really helpful!! big thanks!!!

POSTED BY: pete p

Hi David,

I'm struggling with issues similar to those Pete had. I'm trying to use Tooltip[ListLinePlot [.... constructs within various M10 loop constructs without success yet the same syntax (except for Tooltip) works within Manipulate. Could you please explain, or point me to the right area of documentation that explains, why the higher level plot functions produce no output within a For/While loop. Many thanks David M

POSTED BY: David Mackay
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract