# How to Plot inside a Do loop? [duplicate]

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?

• Just write Table instead of Do Dec 9, 2014 at 21:54
• Or use the nasty Do[Print@Plot[x^n, {x, 1, 2}], {n, -2, 2}] Dec 9, 2014 at 21:57
• All, I'm pretty sure this is a duplicate but I can't find it just yet. Dec 9, 2014 at 22:04
• @verbeia, this one perhaps? Dec 9, 2014 at 22:50
• @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. Dec 9, 2014 at 23:56

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]

• I'm also sure it's a dup, but can't find it. +1 anyway. Dec 9, 2014 at 22:23