0
$\begingroup$

Why does this not give me any plots? It should give five plots.

Do[Plot[x^n, {x, 1, 2}], {n, -2, 2}]

If I remove the Plot from the Do, it works, but I want a series of plots. This is just an example, I'm trying to do something more complicated, but this illustrates the problem.

How can I create a series of plots?

$\endgroup$
  • $\begingroup$ Just write Table instead of Do $\endgroup$ – funnyp0ny Dec 9 '14 at 21:54
  • $\begingroup$ Or use the nasty Do[Print@Plot[x^n, {x, 1, 2}], {n, -2, 2}] $\endgroup$ – Dr. belisarius Dec 9 '14 at 21:57
  • $\begingroup$ All, I'm pretty sure this is a duplicate but I can't find it just yet. $\endgroup$ – Verbeia Dec 9 '14 at 22:04
  • $\begingroup$ @verbeia, this one perhaps? $\endgroup$ – Sjoerd C. de Vries Dec 9 '14 at 22:50
  • $\begingroup$ @SjoerdC.deVries - yes, thanks! Now why couldn't I find it? I feel a bit guilty picking up the rep, but I think my answer is more succinct than the ones to that question. $\endgroup$ – Verbeia Dec 9 '14 at 23:56
4
$\begingroup$

According to the documentation:

Unless an explicit Return is used, the value returned by Do is Null

In other words, unless you create a side effect by assigning something to some expression name, e.g. using Set (=), Do doesn't "do" anything. It evaluates expressions but does not return them.

As mentioned in comments, there are many alternative - and superior - ways to get what you want.

Table[Plot[x^n, {x, 1, 2}], {n, -2, 2}]

Plot[x^#, {x, 1, 2}] & /@ Range[-2, 2]  (* Map *)

Array[Plot[x^#, {x, 1, 2}] &, 5, -2]
|improve this answer|||||
$\endgroup$
  • $\begingroup$ I'm also sure it's a dup, but can't find it. +1 anyway. $\endgroup$ – Dr. belisarius Dec 9 '14 at 22:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.