14

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,
]
\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);
\end{axis}
\end{tikzpicture}
\end{document}

Which gives me:

thing

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

How can I do this?

Zac
  • 315
  • 1
  • 10
  • 1
    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
  • 1
    related: http://tex.stackexchange.com/questions/173602/pgfplots-3d-creating-a-filled-solid-of-revolution/174077#174077 – Christian Feuersänger Nov 15 '14 at 17:13

3 Answers3

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

First try

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.

Ronny
  • 6,110
4

You can improve the answer from Ronny by adding these lines:

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

LCarvalho
  • 1,611
0

enter image description here

\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,
]
\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);

\draw[dashed] (axis cs: 0, {sqrt(1-0.4^2) * cos(deg(0.25pi))}, 0.4) -- (axis cs: 1, {sqrt(1-0.4^2) cos(deg(0.25pi))}, 0.4); \draw[dashed] (axis cs: 1, {sqrt(1-0.4^2) cos(deg(0.25pi))}, 0.4) -- (axis cs: 1, 0, 0.4); \draw[dashed] (axis cs: 1, 0, 0.4) -- (axis cs: 0, 0, 0.4); \draw[dashed] (axis cs: 0, 0, 0.4) -- (axis cs: 0, {sqrt(1-0.4^2) cos(deg(0.25*pi))}, 0.4); \end{axis} \end{tikzpicture} \end{document}

Black Mild
  • 17,569
LCarvalho
  • 1,611