# Plotting with different color for a single curve

How to plot a function $$f(x)=\frac{3(4+x)}{3(2-x)-16}$$ (say $$x \in [-15,15]$$ ) with the condition that i want to give different color for each of the following cases

(i) when $$\frac{x+4}{3x+10}>0$$ and $$\frac{x^2+8x+12}{3x+10}>0$$

(ii) when $$\frac{x+4}{3x+10}>0$$ and $$\frac{x^2+8x+12}{3x+10}<0$$

(iii) when $$\frac{x+4}{3x+10}<0$$ and $$\frac{x^2+8x+12}{3x+10}>0$$

f[x_] := (3 (x + 4))/(3 (2 - x) - 16);

g[x_] := (x + 4)/(3 x + 10);

h[x_] := (x^2 + 8 x + 12)/(3 x + 10);

a[x_] := (g[x] > 0 && h[x] > 0);
b[x_] := (g[x] > 0 && h[x] < 0);
c[x_] := (g[x] < 0 && h[x] > 0);

Plot[{f[x] && a[x], f[x] && b[x], f[x] && c[x]}, {x, -15, 15},
PlotRange -> {-3, 3}, PlotStyle -> Thickness[.01], Frame -> True,
Axes -> False] You could use the option ColorFunction with ColorFunctionScaling->False. First your conditions:

cond1[x_] := (x+4)/(3x+10)>0 && (x^2+8x+12)/(3x+10)>0
cond2[x_] := (x+4)/(3x+10)>0 && (x^2+8x+12)/(3x+10)<0
cond3[x_] := (x+4)/(3x+10)<0 && (x^2+8x+12)/(3x+10)>0


f[x_] := (3(4+x))/(3(2-x)-16)


Then:

Plot[f[x], {x, -15, 15},
PlotRange -> {All, {-3, 3}},
ColorFunctionScaling -> False,
ColorFunction -> Function @ Piecewise[
{
{ColorData, cond1[#]},
{ColorData, cond2[#]},
{ColorData, cond3[#]}
},
ColorData
]
] An alternative way to specify a color function:

cf = ColorData @
(1 + {1, 2}.UnitStep[{(# + 4)/(3 # + 10), (#^2 + 8 # + 12)/(3 # + 10)}]) &;

f[x_] := (3 (4 + x))/(3 (2 - x) - 16)
Plot[f[x], {x, -15, 15},
PlotRange -> {All, {-3, 3}},
BaseStyle -> AbsoluteThickness,
ColorFunction -> cf,
ColorFunctionScaling -> False] • Congratulation in the 200k! Well eine. Jun 22, 2019 at 3:47
• Thank you @user21.
– kglr
Jun 22, 2019 at 3:58

Naïve solution:

f1[x_] /; And[(x + 4)/(3 x + 10) > 0, (x^2 + 8 x + 12)/(3 x + 10) > 0] := (3 (4 + x))/(3 (2 - x) - 16)
f2[x_] /; And[(x + 4)/(3 x + 10) > 0, (x^2 + 8 x + 12)/(3 x + 10) < 0] := (3 (4 + x))/(3 (2 - x) - 16)
f3[x_] /; And[(x + 4)/(3 x + 10) < 0, (x^2 + 8 x + 12)/(3 x + 10) > 0] := (3 (4 + x))/(3 (2 - x) - 16)

Plot[{f1[x], f2[x], f3[x]}, {x, -15, 15}, PlotRange -> {All, {-10, 10}}] 