Message Boards Message Boards

0
|
7257 Views
|
1 Reply
|
1 Total Likes
View groups...
Share
Share this post:

ListPlot does not work in For loop

Posted 9 years ago
list1 = {}
list1 = Append[list1, {1, 2}]
For[i = 1, i <= 2, i++,
 ListPlot[list1, AxesLabel -> {"x", "y"}]
 Print[i]
 ]

It does not print a graph. Is there a way to make it print twice?

Thanks.

POSTED BY: steve ma

This is a not uncommon confusion.

For loops return Null. That is, they don't return whatever is evaluated in them. This is pretty standard behavior across all programming languages. So, to get something out of a for loop, you must use Print, or append something to a list, or something "stateful". You are doing that with "Print[i]".

ListPlot behaves differently than you might expect from other languages. ListPlot doesn't "print" a list plot to the screen. It "returns" a Graphics expression. It's like evaluating "1+1". Evaluating "1+1" doesn't print "2", it returns the value "2". There's a major difference. If you put 1+1 inside the For loop, you'd never see "2" appear either.

Try wrapping "Print" around your "ListPlot".

POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract