# Labelling regions in a CountourPlot3D

Consider a quadratic form in 3 variables: $f(A,B,C)= A^2 + C^2 - B^2 - 4 A B + 2 A C$

I have a plot like so:

plot = ContourPlot3D[
A^2 + C^2 - B^2 - 4 A B + 2 A C == 0, {A, 0, 100}, {B, 0, 100}, {C,
0, 100}, AxesLabel -> Automatic]


I want to label the regions where $f>0$, $f=0$ and $f<0$.

I was trying using Inset but was not able to come up with a proper way to implement it.

Edit 1:

My original question was answered @kglr in the comment. I have a follow up question. What if I now had another such function, say $g(A,B,C)=A^2 + C^2 -9 B^2 - 4 A B + 2 A C$. How to plot and label the four regions:

1. $f>0 , g>0$
2. $f>0 , g<0$
3. $f<0,g>0$
4. $f<0,g<0$
• Show[RegionPlot3D[ 0 <= A <= 100 && 0 <= B <= 100 && 0 <= C <= 100, {A, 0, 100}, {B, 0, 100}, {C, 0, 100}, AxesLabel -> Automatic, BoundaryStyle -> Opacity[0], MeshFunctions -> {#^2 + #3^2 - #2^2 - 4 # #2 + 2 # #3 &}, Mesh -> {{0}}, MeshShading -> {Opacity[.25, Yellow], Opacity[.25, Blue]}], plot, Graphics3D@Text["f > 0", {20, 20, 75}], Graphics3D@Text["f < 0", {80, 80, 25}]]?
– kglr
Commented May 22, 2018 at 5:34
– kglr
Commented May 22, 2018 at 5:53
• @kglr Please see updated question. Commented May 22, 2018 at 6:04
• does this work: ClearAll[f, g] f[a_, b_, c_] := a^2 + c^2 - b^2 - 4 a b + 2 a c; g[a_, b_, c_] := a^2 + c^2 - 9 b^2 - 4 a b + 2 a c; RegionPlot3D[ 0 <= a <= 100 && 0 <= b <= 100 && 0 <= c <= 100, {a, 0, 100}, {b, 0, 100}, {c, 0, 100}, AxesLabel -> Automatic, BoundaryStyle -> None, MeshFunctions -> {f[#, #2, #3] &, g[#, #2, #3] &}, Mesh -> {{0}, {0}}, MeshShading -> {{Opacity[.5, Green], Opacity[.5, Blue]}, { Opacity[.5, Orange], Opacity[.5, Red]}}] for the updated question?
– kglr
Commented May 22, 2018 at 6:27
• @kglr Yeah. They look fine except for the labels. As I am just starting out with Graphics in Mathematica, I need a bit of explanation on MeshFunction. How are the various regions I mentioned in my question identified? Commented May 22, 2018 at 6:43

Update: Labeling the 4 regions defined by 2 functions:

ClearAll[f, g, h]
f = #^2 + #3^2 - #2^2 - 4 # #2 + 2 # #3 &;
g = #^2 + #3^2 - 9 #2^2 - 4 # #2 + 2 # #3 &;
h = And @@ Thread[0 <= {#, #2, #3} <= 100] &;
regions = Tuples[And[#@f[a, b, c] & /@ #, #@g[a, b, c] & /@ #]] &@{Positive, Negative};
labels = {"f > 0\ng > 0", "f > 0\ng < 0", "f < 0\ng > 0", "f < 0\ng < 0"};
SeedRandom[1]
positions = N[Mean /@
({a, b, c} /. FindInstance[# && h[a, b, c], {a, b, c}, Reals, 7] & /@ regions)];
labelsandpositions = Select[Transpose[{labels, positions}], FreeQ[#, a + b + c] &];
insets = Graphics3D[Inset[ Graphics[Text[Style[#, 16]]], #2] & @@@ labelsandpositions];
cp = Show[ContourPlot3D[#[a, b, c] == 0, {a, 0, 100}, {b, 0, 100}, {c, 0, 100},
ContourStyle -> Texture[Graphics[Text[Style[#2, 48, Black]], Background -> #3]]] & @@@
Transpose[{{f, g}, {"f == 0", "g == 0"}, Opacity[.7, #] & /@ {Red, Blue}}]];
rp = RegionPlot3D[h[a, b, c], {a, 0, 100}, {b, 0, 100}, {c, 0, 100},
AxesLabel -> Automatic, BoundaryStyle -> None,
MeshFunctions -> {f, g}, Mesh -> {{0}, {0}},
MeshShading -> {Opacity[.5, #]&/@{Green, Yellow}, Opacity[.5, #]&/@{Purple, Cyan}}];
Show[rp, cp, insets, PlotRange -> All]


Note: For the given functions f and g the region f < 0 && g > 0 is empty.

ClearAll[f, h, a, b, c]
f = #^2 + #3^2 - #2^2 - 4 # #2 + 2 # #3 &;
h = 0 <= # <= 100 && 0 <= #2 <= 100 && 0 <= #3 <= 100 &;
plot = ContourPlot3D[f[a, b, c] == 0, {a, 0, 100}, {b, 0, 100}, {c, 0, 100},
ContourStyle -> Texture[Graphics[Text[Style["f = 0", 64, Cyan]], Background -> Red]]];
Show[RegionPlot3D[h[a, b, c], {a, 0, 100}, {b, 0, 100}, {c, 0, 100},
AxesLabel -> Automatic, BoundaryStyle -> None,
MeshFunctions -> {f}, Mesh -> {{0}},
MeshShading -> {Opacity[.25, Yellow], Opacity[.25, Blue]}],
plot, Graphics3D[{Inset[Graphics[Text[Style["f > 0", 20]]], {25, 25, 85}],
Inset[Graphics[Text[Style["f < 0", 20]]], {85, 85, 25}]}]]


• Really nice implementation! Could you check my updated question? Commented May 22, 2018 at 6:16
• When saving as an eps file, the "f=0" text turns into a solid blue rectangle. Any fix for that? Commented May 22, 2018 at 7:27
• @Subho95, can think of a fix; you might want to check related q/a's on this site
– kglr
Commented May 22, 2018 at 7:38