7
$\begingroup$

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:

enter image description here

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:

enter image description here

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

Somebody, please, could enlighten me? Thank you!

$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Commented Jan 24, 2017 at 11:59
  • 2
    $\begingroup$ @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.) $\endgroup$
    – Michael E2
    Commented Jan 24, 2017 at 12:57

3 Answers 3

6
$\begingroup$

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:

enter image description here

which is exactly what I want.

$\endgroup$
0
5
$\begingroup$

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

BarLegend["Rainbow"]

enter image description here

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}}]]

enter image description here

$\endgroup$
4
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Commented Jan 24, 2017 at 12:03
  • 1
    $\begingroup$ @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). $\endgroup$
    – Michael E2
    Commented Jan 24, 2017 at 13:08
  • 1
    $\begingroup$ @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). $\endgroup$
    – Mr.Wizard
    Commented Jan 24, 2017 at 14:21
  • 1
    $\begingroup$ @Manu I updated my answer to at least be similar to what you want. $\endgroup$
    – Mr.Wizard
    Commented Jan 24, 2017 at 14:36
2
$\begingroup$

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]}]]
 ]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ Is it just me, or are those moiré fringes? $\endgroup$ Commented Jan 24, 2017 at 15:56
  • $\begingroup$ 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. $\endgroup$
    – Michael E2
    Commented Jan 24, 2017 at 21:53

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.