Message Boards Message Boards


ListPlot does not work in For loop

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

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


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
or Discard

Group Abstract Group Abstract