Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by Mr.Wizard Feb 16 at 8:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
You could use Show[plot1, plot2] where each plot has only one function with the corresponding options. –  b.gatessucks Feb 15 at 22:40
    
Related: (1128), (8199), (16262), (19004), (22571), (42235) –  Mr.Wizard Feb 16 at 8:09
add comment

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

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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