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?

2

3 Answers 3

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.

4
  • 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. Commented May 4, 2017 at 21:09
  • You're right; but that 3rd plane can be copy pasted from the other ones :)
    – Ronny
    Commented May 5, 2017 at 5:13
  • @Ronny - Is it possible to draw 2D shapes on this 3D structure? Commented May 7, 2017 at 20:18
  • That depends; but I think in most cases – yes :) better open a new question for that.
    – Ronny
    Commented May 8, 2017 at 4:11
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

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.25*pi))}, 0.4) -- (axis cs: 1, {sqrt(1-0.4^2) * cos(deg(0.25*pi))}, 0.4);
\draw[dashed] (axis cs: 1, {sqrt(1-0.4^2) * cos(deg(0.25*pi))}, 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}
1
  • What's the difference between this and your other answer?
    – Joseph Wright
    Commented Sep 27, 2023 at 8:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .