5
$\begingroup$

How can I plot the same function with different parameters?

I want to get something like this:

For[i = 0, i < 4, i++, Show[Plot[Sin[x] + i, {x, 0, 2 Pi}]]]. 

This is just an example.

$\endgroup$
6
$\begingroup$

This is an alternative, perhaps more convenient depending on what you want to do with plot.

Plot[Evaluate[Sin[x] + # & /@ list], {x, 0, 2 Pi}, 
 PlotRange -> {0, 7}, PlotLegends -> Automatic, PlotLegends -> 
 LineLegend[Automatic,LegendFunction -> (Framed[#1, FrameMargins -> 0]&)]]

enter image description here

$\endgroup$
5
$\begingroup$

You can also use Table,

Plot[Evaluate@Table[Sin[x] + i, {i, 1, 6}], {x, 0, 2 Pi}, PlotRange -> {0, 7}]

enter image description here

Or use SetDelay plus Range,

f[i_] := Sin[x] + i

Plot[Evaluate[f /@ Range[6]], {x, 0, 2 Pi}, PlotRange -> {0, 7}]
$\endgroup$
4
$\begingroup$

You can use mapping instead of a for loop:

For instance, define the list of parameter to be changed:

list = {1, 2, 3, 4, 5, 6};

Then:

Show[Plot[Sin[x] + #, {x, 0, 2 Pi}, PlotRange -> {0, 7}] & /@ list]

enter image description here

Have a look at how mapping works: http://reference.wolfram.com/language/ref/Map.html

It is very useful to write clean code.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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