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?


closed as off-topic by m_goldberg, MarcoB, Alex Trounev, happy fish, C. E. May 1 at 12:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – MarcoB, Alex Trounev, happy fish, C. E.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Why not just func[expr_, var_] := Plot[expr, {var, 0, 10}, PlotStyle -> {{Red}, {Black}}]? $\endgroup$ – J. M. will be back soon Mar 31 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$ – Innerw0lf Mar 31 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


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