I have a function func[expr,var] with which I want to plot the list expr[var] and have different styles applied to it. When doing this with ListPlot, it works, but somehow this just gives the plot with the first PlotStyle given.

To be specific:

func[expr_, var_] := Module[{f}, f[x_] := Evaluate[expr /. var -> x]; Plot[f[x], {x, 0, 10}, PlotStyle -> {{Red}, {Black}}]]

This gives the plot of Sin[x] and Sin[2x] both in red. How do I get one the first expression in red and the second in black?

  • $\begingroup$ Why not just func[expr_, var_] := Plot[expr, {var, 0, 10}, PlotStyle -> {{Red}, {Black}}]? $\endgroup$ – J. M.'s discontentment Mar 31 '19 at 22:48
  • $\begingroup$ True :'D I must've somehow come to the conclusion that this would not work for some reason. Now I changed quite a bit in the structure and this seems to work again. Thanks $\endgroup$ – Innerwolf Mar 31 '19 at 22:52

Keep f as a list of functions, rather than a new function f[x].

func[expr_, var_] := Module[{f, x},
  f = expr /. var -> x;
  Plot[f, {x, 0, 10}, PlotStyle -> {Red, Black}]]

func[{Sin[y], Sin[2 y]}, y]

enter image description here

| improve this answer | |

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