0

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?

Verbeia
  • 34,233
  • 9
  • 109
  • 224
Jerry Guern
  • 4,602
  • 18
  • 47

1 Answers1

4

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]
Verbeia
  • 34,233
  • 9
  • 109
  • 224