使用 pgfplots 绘制球体的封闭部分

使用 pgfplots 绘制球体的封闭部分

我想画一个球体的剖面来表示它的体积。

我目前得到以下信息:

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

这给了我:

事物

但我希望两侧能够闭合,如下所示: 事物2

我怎样才能做到这一点?

答案1

第一种方法(虽然简单)是将所有 3 个侧面视为表面本身。因此,只需将一个或另一个组件设置为 0,就可以获得

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

第一次尝试

我必须以正确的方式对它们进行排序,因为它们彼此之间没有 z 缓冲。而且 - 在我看来 - 使用标准颜色图可能会误导生成的图像的 3D 效果。

答案2

你可以改进答案罗尼添加以下几行:

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

通过这些线条,标签将更加接近:

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

解决方法

答案3

在此处输入图片描述

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

相关内容