Multiple curves plotted with different colours [duplicate]

This question already has an answer here:

How do you plot the same function with different options over different intervals?

5 answers

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?

=================

=================

2 Answers
2

=================

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}]