# Problems with ImplictRegion

Through code:

f[x_, y_, z_] = x^2 + y^2 - z^2;

RegionPlot3D[
x^2 + y^2 - z^2 <= 1 && 0 <= z <= 1,
{x, -2, 2}, {y, -2, 2}, {z, 0, 1},
Axes -> True,
AxesLabel -> {x, y, z},
ColorFunction -> Function[{x, y, z},
ColorData["Rainbow"][f[x, y, z]]],
ColorFunctionScaling -> False,
MeshFunctions -> Function[{x, y, z}, f[x, y, z]],
BoxRatios -> {1, 1, 1},
PlotPoints -> 75,
PlotLegends -> BarLegend["Rainbow"]]


I get: but even if the maximum value is properly 1 for x^2+y^2-z^2=1, the minimum value should be -1 for (0, 0, 1), not 0! What am I doing wrong?

In addition, through the code (which I think is more elegant):

f[x_, y_, z_] = x^2 + y^2 - z^2;

A = ImplicitRegion[x^2 + y^2 - z^2 <= 1 && 0 <= z <= 1, {x, y, z}];

RegionPlot3D[
A,
Axes -> True,
AxesLabel -> {x, y, z},
ColorFunction -> Function[{x, y, z},
ColorData["Rainbow"][f[x, y, z]]],
ColorFunctionScaling -> False,
MeshFunctions -> Function[{x, y, z}, f[x, y, z]],
BoxRatios -> {1, 1, 1},
PlotPoints -> 75,
PlotLegends -> BarLegend["Rainbow"]]


I get: and here I really do not understand anything! Why this mess?

Somebody, please, could enlighten me? Thank you!

• I looked at the ImplicitRegion case but I don't understand it. Hopefully someone else does and answers that soon. If not I'll look again later. – Mr.Wizard Jan 24 '17 at 11:59
• @Mr.Wizard RegionPlot*[region] is not well documented and does not really behave like RegionPlot*[inequalities]. Right now, it seems to be an interface to DiscretizeRegion and MeshRegion styling. I don't know what WRI's intentions are, and whether they plan to implement such things as Mesh and other styling options. (I'm assuming that since it's still undocumented, they're working on it. They seem to acknowledge that there is still work to be done on regions, but I don't know how much of a priority plotting is. Manu, you might report it; requests from clients can affect priorities.) – Michael E2 Jan 24 '17 at 12:57

## 3 Answers

One possible solution is the following code:

f[x_, y_, z_] = x^2 + y^2 - z^2;

A = ImplicitRegion[x^2 + y^2 - z^2 <= 1 && 0 <= z <= 1, {x, y, z}];

SliceDensityPlot3D[
f[x, y, z],
BoundaryDiscretizeRegion[A],
{x, y, z} \[Element] DiscretizeRegion[A],
ColorFunction -> "Rainbow",
AxesLabel -> Automatic,
PlotLegends -> Automatic]


I get: which is exactly what I want.

• In this case, the function f has the absolute minimum and maximum points on the boundary of A, then you will see perfectly. But if these points were in the interior of A, as you might view them? – TeM Jan 24 '17 at 14:45

BarLegend["Rainbow"] gives a generic bar over a range of [0, 1].

BarLegend["Rainbow"] It appears that for RegionPlot3D you will need to specify the range manually.

Update: adjusted to match your target plot from your own solution.

f[x_, y_, z_] := x^2 + y^2 - z^2;

color = ColorData[{"Rainbow", {-1, 1}}];

RegionPlot3D[f[x, y, z] <= 1 && 0 <= z <= 1, {x, -2, 2}, {y, -2, 2}, {z, 0, 1},
Axes -> True, AxesLabel -> {x, y, z}, ColorFunction -> (color @ f[#, #2, #3] &),
ColorFunctionScaling -> False, Mesh -> False,
BoxRatios -> {1, 1, 1}, PlotPoints -> 75, PlotLegends -> BarLegend[{color, {-1, 1}}]] • @Manu In all honestly I haven't done much with these functions in recent years (since the addition of the legending functions) so I may be behind the times as it were. I still hope for a better answer, including addressing of the ImplicitRegion case. – Mr.Wizard Jan 24 '17 at 12:03
• @Manu IMO, what should happen is that PlotLegends -> Automatic would do exactly what you want, just as it does with ContourPlot (and according to the documentation for BarLegends, although no examples use RegionPlot). – Michael E2 Jan 24 '17 at 13:08
• @Manu I recommend posting your last update as a self-answer. There are still questions remaining which may in time be answered, so I wouldn't recommend accepting it (or mine for that matter). – Mr.Wizard Jan 24 '17 at 14:21
• @Manu I updated my answer to at least be similar to what you want. – Mr.Wizard Jan 24 '17 at 14:36

It appears that PlotLegends -> Automatic pulls the ColorFunction settings only for ColorFunction -> "name" and not for custom settings. Here's a way to somewhat automate the construction of the legend, by using Sow/Reap to get the range of the values of f[x, y, z] in the ColorFunction:

f[x_, y_, z_] = x^2 + y^2 - z^2;

Module[{plot, vals, $val}, {plot, vals} = Reap[ RegionPlot3D[x^2 + y^2 - z^2 <= 1 && 0 <= z <= 1, {x, -2, 2}, {y, -2, 2}, {z, 0, 1}, Axes -> True, AxesLabel -> {x, y, z}, ColorFunction -> Function[{x, y, z}, ColorData["Rainbow"][Sow[f[x, y, z],$val]]],
ColorFunctionScaling -> False,
MeshFunctions -> Function[{x, y, z}, f[x, y, z]],
BoxRatios -> {1, 1, 1}, PlotPoints -> 75],
\$val];
Legended[plot, BarLegend[{"Rainbow", MinMax[vals]}]]
] • Is it just me, or are those moiré fringes? – J. M.'s ennui Jan 24 '17 at 15:56
• The Moiré effect came from the rasterization. Kinda annoying, but I could not get rid of it (easily). I think it's from the OP's mesh-function lines. – Michael E2 Jan 24 '17 at 21:53