1
$\begingroup$

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:

enter image description here

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

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

2 Answers 2

5
$\begingroup$

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

Mathematica graphics

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.

$\endgroup$
2
$\begingroup$

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

plot

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.