# Draw closed section of sphere using pgfplots

I want to draw a section of a sphere to represent its volume.

I have currently got the following:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis equal,
axis lines = center,
width = 10cm,
height = 10cm,
xlabel = {$x$},
ylabel = {$y$},
zlabel = {$z$},
view/h=45,
]
samples=21,
domain=0:1,
y domain=0:0.5*pi,
z buffer=sort]
({sqrt(1-x^2) * cos(deg(y))},
{sqrt( 1-x^2 ) * sin(deg(y))},
x);
\end{axis}
\end{tikzpicture}
\end{document}


Which gives me:

But I want the sides to be closed, something like this:

How can I do this?

• Welcome to TeX SE! It is great you've provided code. Could you complete it so potential helpers can copy-paste a compilable document? – cfr Nov 15 '14 at 15:11
• – Christian Feuersänger Nov 15 '14 at 17:13

A first - though simple - approach - would be to treat all 3 sides as surfaces themselves. So by just setting one or another component to 0, one would obtain

\documentclass{article}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis equal,
axis lines = center,
width = 10cm,
height = 10cm,
xlabel = {$x$},
ylabel = {$y$},
zlabel = {$z$},
view/h=45,
]
samples=21,domain=0:1,
y domain=0:0.5*pi,z buffer=sort]
(0, {sqrt( 1-x^2 ) * sin(deg(y))}, x);
samples=21, domain=0:1, y domain=0:0.5*pi, z buffer=sort]
({sqrt(1-x^2) * cos(deg(y))}, {sqrt( 1-x^2 ) * sin(deg(y))}, x);
samples=21, domain=0:1, y domain=0:0.5*pi, z buffer=sort]
({sqrt(1-x^2) * cos(deg(y))},0, x);
\end{axis}
\end{tikzpicture}
\end{document}


I had to order them the right way, because they aren't z-buffered with respect to each other. And - in my opinion - using the standard color map might be misleading in the resulting images 3d effect.

• I think Ronny forgot the bottom surface here. If you compare his finished plot with the grayscale one of the OP's question, the bottom panel is absent. – stars83clouds May 4 '17 at 21:09
• You're right; but that 3rd plane can be copy pasted from the other ones :) – Ronny May 5 '17 at 5:13
• @Ronny - Is it possible to draw 2D shapes on this 3D structure? – stars83clouds May 7 '17 at 20:18
• That depends; but I think in most cases – yes :) better open a new question for that. – Ronny May 8 '17 at 4:11

every axis x label/.style={ at={(ticklabel* cs:1.05)}},
every axis y label/.style={ at={(ticklabel* cs:1.05)}},
every axis z label/.style={ at={(ticklabel* cs:1.05)}}


With these lines the labels will be closer:

\documentclass{article}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis equal,
axis lines = center,
width = 10cm,
height = 10cm,
xlabel = {$x$},
ylabel = {$y$},
zlabel = {$z$},
zmax=1.1,
view/h=45,
every axis x label/.style={ at={(ticklabel* cs:1.05)}},
every axis y label/.style={ at={(ticklabel* cs:1.05)}},
every axis z label/.style={ at={(ticklabel* cs:1.05)}}
]
\addplot3[surf, opacity = 0.5,samples=21, domain=0:1, y domain=0:0.5*pi, z buffer=sort](0, {sqrt( 1-x^2 ) * sin(deg(y))}, x);
\addplot3[surf, opacity = 0.5,samples=21, domain=0:1, y domain=0:0.5*pi, z buffer=sort]({sqrt(1-x^2) * cos(deg(y))}, {sqrt( 1-x^2 ) * sin(deg(y))}, x);
\addplot3[surf, opacity = 0.5,samples=21, domain=0:1, y domain=0:0.5*pi, z buffer=sort]({sqrt(1-x^2) * cos(deg(y))},0, x);
\end{axis}
\end{tikzpicture}
\end{document}


Solution by JPi