# RegionPlot3D in cylindrical or spherical coordinates?

RegionPlot3D is awesome. Unfortunately, it appears to work only for Cartesian coordinates. While there are ways to draw surfaces in spherical or cylindrical coordinates, I can't find a way to draw solids. Is there a way to do so that doesn't involve translating the equations to Cartesian coordinates? Thanks.

You can always hide away the coordinate transformations inside a function that calls RegionPlot3D. Here's a quick & dirty sphericalRegionPlot3D:

sphericalRegionPlot3D[
ineq_, {r_, rmin_: 0, rmax_: 1}, {th_, thmin_: 0, thmax_}, {ph_,
phmin_, phmax_}, opts___] := RegionPlot3D[With[{
r = Sqrt[x^2 + y^2 + z^2],
th = ArcCos[z/Sqrt[x^2 + y^2 + z^2]],
ph = ArcTan[x, y] + Pi},
ineq && rmin <= r <= rmax && thmin <= th <= thmax &&
phmin <= ph <= phmax],
{x, -rmax, rmax}, {y, -rmax, rmax}, {z, -rmax, rmax},
MeshFunctions -> {Sqrt[#1^2 + #2^2 + #3^2] &, ArcTan[#1, #2] &,
ArcCos[#3/Sqrt[#1^2 + #2^2 + #3^2]] &}, opts];

SetAttributes[sphericalRegionPlot3D, HoldAll];


Example:

sphericalRegionPlot3D[
Mod[ph, Pi/3] < Pi/6, {r, 2, 3}, {th, 1, Pi}, {ph, 0, 2 Pi},
PlotPoints -> 100, Mesh -> {3, 60, 30}] • And, it should work in V8 or even earlier. – Mark McClure Dec 16 '12 at 20:39
• I want to upvote this answer but I don't like the coding. What's going on with r, th, ph? You never localized these. Yet, you did localize x, y, z which you should not have as these are already localized by RegionPlot3D. I know you can do better. – Mr.Wizard Dec 16 '12 at 23:50

While not positive, I believe the answer is that RegionPlot does not support spherical (or other non-Cartesian) coordinates natively.

If correct, I guess the question becomes "What's the easiest way to plot a region defined in terms of spherical coordinates, without resorting to converting the equations by hand?" V9 has commands to ease this process.

Here are a couple of examples that show how you can use the TransformedField field command to convert your expression and you can use CoordinateTransform to generate a mesh that reflects the coordinate system that you want.

sphericalExpression = 1 + rho^2 - 2 rho*Cos[theta] Sin[phi];
cartesianExpression =
TransformedField["Spherical" -> "Cartesian",
sphericalExpression, {rho, phi, theta} -> {x, y, z}]
meshFunctions = Function /@ CoordinateTransform[
"Cartesian" -> "Spherical", {#1, #2, #3}]
RegionPlot3D[cartesianExpression < 1, {x, 0, 2}, {y, -1, 1}, {z, -1, 1},
MeshFunctions -> meshFunctions, PlotPoints -> 50, MaxRecursion -> 8,
ViewPoint -> {2, 0, 3}, Boxed -> False,
AxesEdge -> {{-1, -1}, {1, -1}, {1, -1}}] I've cranked up PlotPoints and MaxRecursion since mesh lines are fairly complicated on the other side of the figure.

Show[%, ViewPoint -> {-3, 0, 2}] That example was chosen to yield a simple expression in Cartesian coordinates. Arbitrary expressions might yield numerical warnings but generally work. If you'd like to constrain your figure to lie within a sphere, you can do so by including a x^2+y^2+z^2<1 in your region specification.

sphericalExpression = Sin[rho] + Cos[theta]*phi;
cartesianExpression =
TransformedField["Spherical" -> "Cartesian",
sphericalExpression, {rho, phi, theta} -> {x, y, z}] // Simplify
RegionPlot3D[
cartesianExpression < 1 && x^2 + y^2 + z^2 < 1, {x, -1, 1}, {y, -1,
1}, {z, -1, 1}, MaxRecursion -> 5, PlotPoints -> 50,
MeshFunctions -> meshFunctions] // Quiet • Right. I think the answer is: "no, that capability is not explicitly supported", although I am not so certain of that that I wanted to put it in my answer - particularly, given V9's new capabilities. Thus, my answer addresses the simplest way I know of to go from a rho, phi, theta expression to a region plot. – Mark McClure Dec 16 '12 at 16:05
• Thanks. I don't have 9 yet, but perhaps I will download it for this capability. Really too bad that the 3D graphics set is not symmetric around usual choices of coordinate systems. – rogerl Dec 16 '12 at 16:27
• @rogerl I had the same interpretation of your question as Nasser. Is that indeed what you intended? – Sjoerd C. de Vries Dec 16 '12 at 16:55
• Yes, that is what I intended. I think this answer is responsive to the intent of my question. – rogerl Dec 16 '12 at 17:48
• @rogerl Clearly, the situation could be better. It should be possible, at least, to draw mesh lines that are indicative to your chosen coordinate system. Would that be of interest? – Mark McClure Dec 16 '12 at 17:53

More generalized version:

 newRegionPlot3D[
expr_, {u_, umin_, umax_}, {v_, vmin_, vmax_}, {w_, wmin_, wmax_}, tr_, opts___] :=
Module[{x, y, z, newExpr, xyz},
newExpr = TransformedField[tr -> "Cartesian", expr, {u, v, w} -> {x, y, z}];
xyz = CoordinateTransform[tr -> "Cartesian", {u, v, w}];
{xmax, ymax, zmax} = NMaxValue[{#,
umin < u < umax && vmin < v < vmax && wmin < w < wmax}, {u, v, w}] & /@ xyz;
{xmin, ymin, zmin} = NMinValue[{#, umin < u < umax && vmin < v < vmax && wmin < w < wmax}, {u, v, w}] & /@ xyz;
RegionPlot3D[
newExpr, {x, xmin, xmax}, {y, ymin, ymax}, {z, zmin, zmax}, opts]
]


Some examples:

newRegionPlot3D[r < 1, {r, 0, 1}, {phi, 0, 2 Pi}, {th, 0, Pi}, "Spherical"]
newRegionPlot3D[ro < 1, {ro, 0, 1}, {phi, 0, 2 Pi}, {z, 0, 1}, "Cylindrical"] • your generalized version is exactly what I was looking for. Thanks a lot! – Alex May 1 '15 at 18:07