1
$\begingroup$

Is there a way to plot a simple diagram in Mathematica with colored areas, like a phase diagram, for the different ranges of values of a function such $f(a,b)=(1-a)/(b+2)$, for various combinations of $a$ & $b$?

I mean, for example, if $0<a<1$ and $0<b<2$, plot a diagram where the x-axis is $a$, and the y-axis is $b$, in which the area where $1/4<f<1/2$ is colored red, and $0<f<1/4$ is colored blue and the rest is white?

$\endgroup$

4 Answers 4

1
$\begingroup$

Try RegionPlot(I changed your example a little bit because of 0<f<1/2)

Show[{
RegionPlot[0 < (1 - a)/(2 + b) < 1/4, {a, 0, 1}, {b, 0, 2},PlotStyle -> Blue],
RegionPlot[1/4 < (1 - a)/(2 + b) < 1/2, {a, 0, 1}, {b, 0, 2},PlotStyle -> Red]
}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Great. Thank you! I have edited my question 'for the educational purpose', in-order to fit with your observation about f. $\endgroup$ Sep 18, 2019 at 15:02
2
$\begingroup$

You can use ContourPlot, specify the desired contours, and also use a non-default ColorFunction option. I also needed to use ColorFunctionScaling so that function values are left alone.

f[a_,b_] := (1-a)/(b+2)

ContourPlot[f[a, b], {a, 0, 1}, {b, 0, 2},
    Contours->{0, 1/4, 1/2},
    ColorFunction -> Function @ Piecewise[{{Red, 1/4<#<1/2}, {Blue, 0<#<1/4}}, White],
    ColorFunctionScaling -> False
]

enter image description here

$\endgroup$
1
  • $\begingroup$ Yes, thanks. Also works well. $\endgroup$ Sep 18, 2019 at 15:15
2
$\begingroup$
f[a_, b_] := (1 - a)/(b + 2)

You can use a single RegionPlot:

RegionPlot[{0< f[a,b] <= 1/4, 1/4 < f[a,b] <= 1/2}, {a, 0, 1}, {b, 0, 2},
  PlotStyle ->{Blue, Red}, BoundaryStyle -> None]

enter image description here

Alternatively, RegionPlot with the options MeshFunctions+Mesh+ MeshShading:

RegionPlot[True, {a, 0, 1}, {b, 0, 2},
  MeshFunctions -> {f[#, #2] &}, 
  Mesh -> {{1/4, 1/2}},
  MeshShading -> {Blue, Red, White}]

enter image description here

ContourPlot with the option ContourShading:

ContourPlot[f[a, b], {a, 0, 1}, {b, 0, 2},
  Contours -> { 1/4, 1/2},
  ContourShading -> {Blue, Red, White}]

same picture

$\endgroup$
0
$\begingroup$

Another alternative, using Filling

f[a_, b_] = (1 - a)/(2 + b);

boundary = b /. Solve[f[a, b] == 1/4, b][[1]]

(* -2 (-1 + 2 a) *)

Show[
 Plot[boundary, {a, 0, 1},
    PlotRange -> {0, 2},
    Frame -> True,
    Filling -> {1 -> #}] & /@
  {{Bottom, Red}, {Top, Blue}},
 AspectRatio -> 1,
 FrameLabel -> (Style[#, Bold, 14] & /@ {a, b})]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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