Qiang Li
Qiang Li

Reputation: 10865

Show plots inside module in Mathematica

I want to show plots inside a module (maybe recursive):

m = Module[{i, j}, i = 3; Plot[Sin[t], {t, 0, 1}]; j = 4]

Even

m = Module[{i, j}, i = 3; Show[Plot[Sin[t], {t, 0, 1}]]; j = 4]

not work. Why is this, and how to plot correctly?

Upvotes: 8

Views: 5824

Answers (1)

David Z
David Z

Reputation: 131740

The only reason a plot is normally displayed in Mathematica is that the Plot function returns the graphics object representing the plot, and Mathematica displays the return value of whatever you run in a notebook. However, when you follow the statement with a semicolon, you prevent it from returning a value.

What you can do if you need to display something from within the middle of a module is Print[Plot[...]];. The Print function displays the value of its argument directly.

Upvotes: 16

Related Questions