# Multiple curves plotted with different colours [duplicate]

I am trying to use ColorFunction for 2 functions on the same plot. I only want the cubic equation to change colour.

Plot[{-x^3 + 3.5*x + 0.5, y = 2.9*Tanh[5 x] + 0.3}, {x, -2.5, 2.5},
PlotRange -> {-3, 3.5}, Axes -> False, Frame -> True, FrameTicks -> None,
PlotStyle -> {{Darker[Green], Thick}, {Blue, Thick}},
ColorFunction -> Function[{x, y}, If[x > 1, Red, Black]],
ColorFunctionScaling -> False, PlotStyle -> Thick]


This is what the code above produces:

This is ideally how i would like it to be displayed. Is this possible in Mathematica without using the drawing tool?

• You could use Show[plot1, plot2] where each plot has only one function with the corresponding options. Commented Feb 15, 2014 at 22:40
• Related: (1128), (8199), (16262), (19004), (22571), (42235) Commented Feb 16, 2014 at 8:09

MeshFunctions is useful for discrete changes in color in Plot:

Show[
Plot[2.9*Tanh[5 x] + 0.3, {x, -2.5, 2.5}, PlotRange -> {-3, 3.5},
Axes -> False, Frame -> True, FrameTicks -> None,
PlotStyle -> {Blue, Thick}],
Plot[-x^3 + 3.5*x + 0.5, {x, -2.5, 2.5}, Mesh -> {{-1, 1}},
MeshShading -> {Darker@Green, Black}, PlotStyle -> Thick]
]


Change the numbers in Mesh -> {{-1, 1}} to move the transition points. Change the colors in MeshShading -> {Darker@Green, Black} to get a different arrangement of colors.

As b.gatessucks writes in his comment, you want make two plots and combine them with Show. Also you need to modify your color function a little.

p1 = Plot[-x^3 + 3.5*x + 0.5, {x, -2.5, 2.5},
PlotRange -> {-3, 3.5},
Axes -> False,
Frame -> True,
FrameTicks -> None,
PlotStyle -> Thick,
ColorFunction -> (If[Abs[#] > 1, RGBColor[0, .6667, 0], RGBColor[0, 0, 0]] &),
ColorFunctionScaling -> None];
p2 = Plot[2.9*Tanh[5 x] + 0.3, {x, -2.5, 2.5},
PlotRange -> {-3, 3.5},
PlotStyle -> {Blue, Thick}];
Show[{p1, p2}]