Skip to main content
Fixed format of link
Source Link
Esme_
  • 693
  • 4
  • 13

I am trying to adapt [this solution for colouring the different sections of a Piecewise function]https://mathematica.stackexchange.com/a/1132/34694 [this solution for colouring the different sections of a Piecewise function].

I want to colour based on the x-axis value. I have two functions I want to do this for, one is Piecewise the other can be written as Piecewise.

MWE

a[x_]:= x^2;
b[x_] = Log[x];
f = Piecewise[{{a[x], 0<= x<= 3}, {b[x], 3<x <= 7}}]

g = Piecewise[{{#^2, # <= 0}, {Log[#], 2 < #}}] &

colorFunction = f
piecewiseParts = Length@colorFunction[[1, 1]]
colors =  {Red, Green}
colorFunction[[1, 1, All, 1]] = colors

Plot[f, {x, -2, 4}, ColorFunction -> colorFunction, 
 ColorFunctionScaling -> False]

The linked example uses a Piecewise function defined like g (use g everywhere instead of g) - I need to use something that is formed like f.

I am trying to adapt [this solution for colouring the different sections of a Piecewise function]https://mathematica.stackexchange.com/a/1132/34694.

I want to colour based on the x-axis value. I have two functions I want to do this for, one is Piecewise the other can be written as Piecewise.

MWE

a[x_]:= x^2;
b[x_] = Log[x];
f = Piecewise[{{a[x], 0<= x<= 3}, {b[x], 3<x <= 7}}]

g = Piecewise[{{#^2, # <= 0}, {Log[#], 2 < #}}] &

colorFunction = f
piecewiseParts = Length@colorFunction[[1, 1]]
colors =  {Red, Green}
colorFunction[[1, 1, All, 1]] = colors

Plot[f, {x, -2, 4}, ColorFunction -> colorFunction, 
 ColorFunctionScaling -> False]

The linked example uses a Piecewise function defined like g (use g everywhere instead of g) - I need to use something that is formed like f.

I am trying to adapt [this solution for colouring the different sections of a Piecewise function]

I want to colour based on the x-axis value. I have two functions I want to do this for, one is Piecewise the other can be written as Piecewise.

MWE

a[x_]:= x^2;
b[x_] = Log[x];
f = Piecewise[{{a[x], 0<= x<= 3}, {b[x], 3<x <= 7}}]

g = Piecewise[{{#^2, # <= 0}, {Log[#], 2 < #}}] &

colorFunction = f
piecewiseParts = Length@colorFunction[[1, 1]]
colors =  {Red, Green}
colorFunction[[1, 1, All, 1]] = colors

Plot[f, {x, -2, 4}, ColorFunction -> colorFunction, 
 ColorFunctionScaling -> False]

The linked example uses a Piecewise function defined like g (use g everywhere instead of g) - I need to use something that is formed like f.

Source Link
Esme_
  • 693
  • 4
  • 13

Colour plot based on x-axis value

I am trying to adapt [this solution for colouring the different sections of a Piecewise function]https://mathematica.stackexchange.com/a/1132/34694.

I want to colour based on the x-axis value. I have two functions I want to do this for, one is Piecewise the other can be written as Piecewise.

MWE

a[x_]:= x^2;
b[x_] = Log[x];
f = Piecewise[{{a[x], 0<= x<= 3}, {b[x], 3<x <= 7}}]

g = Piecewise[{{#^2, # <= 0}, {Log[#], 2 < #}}] &

colorFunction = f
piecewiseParts = Length@colorFunction[[1, 1]]
colors =  {Red, Green}
colorFunction[[1, 1, All, 1]] = colors

Plot[f, {x, -2, 4}, ColorFunction -> colorFunction, 
 ColorFunctionScaling -> False]

The linked example uses a Piecewise function defined like g (use g everywhere instead of g) - I need to use something that is formed like f.