# Sphere Segments in PGF/TIKZ

I want to draw sphere segments, I made a sketch here:

An optional bonus would be to separate the segments slightly in x1 direction. Any help would be appreciated!

All I am doing here is to apply the IMHO extremely neat macros from this great answer.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\usepackage{xxcolor}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
% Declare nice sphere shading: http://tex.stackexchange.com/a/54239/12440
color(0bp)=(tikz@ball!0!white);
color(7bp)=(tikz@ball!0!white);
color(15bp)=(tikz@ball!70!black);
color(20bp)=(black!70);
color(30bp)=(black!70)}
\makeatother

% Style to set TikZ camera angle, like PGFPlots view
\tikzset{viewport/.style 2 args={
x={({cos(-#1)*1cm},{sin(-#1)*sin(#2)*1cm})},
y={({-sin(-#1)*1cm},{cos(-#1)*sin(#2)*1cm})},
z={(0,{cos(#2)*1cm})}
}}

% Styles to plot only points that are before or behind the sphere.
\pgfplotsset{only foreground/.style={
restrict expr to domain={rawx*\CameraX + rawy*\CameraY + rawz*\CameraZ}{-0.05:100},
}}
\pgfplotsset{only background/.style={
restrict expr to domain={rawx*\CameraX + rawy*\CameraY + rawz*\CameraZ}{-100:0.05}
}}

% Automatically plot transparent lines in background and solid lines in foreground
}

\newcommand{\ViewAzimuth}{-20}
\newcommand{\ViewElevation}{15}

\begin{document}
\begin{tikzpicture}[rotate=-90]
% Compute camera unit vector for calculating depth
\pgfmathsetmacro{\CameraX}{sin(\ViewAzimuth)*cos(\ViewElevation)}
\pgfmathsetmacro{\CameraY}{-cos(\ViewAzimuth)*cos(\ViewElevation)}
\pgfmathsetmacro{\CameraZ}{sin(\ViewElevation)}
\pgfmathsetmacro{\DeltaPhi}{10}
% Draw a nice looking sphere
\begin{scope}
\clip[name path global=sphere] (0,0) circle (\Radius*1cm);
\begin{scope}[transform canvas={rotate=-200}]
\end{scope}
\end{scope}
\begin{axis}[clip=false,
hide axis,
view={\ViewAzimuth}{\ViewElevation},     % Set view angle
every axis plot/.style={very thin},
disabledatascaling,                      % Align PGFPlots coordinates with TikZ
anchor=origin,                           % Align PGFPlots coordinates with TikZ
viewport={\ViewAzimuth}{\ViewElevation}, % Align PGFPlots coordinates with TikZ
]
% draw axis by hand
\draw[dashed,name intersections={of=xaxis and sphere,by=X}]
(0,0,0) --  (X);
\draw[dashed,name intersections={of=yaxis and sphere,by=Y}]
(0,0,0) --  (Y);
% Plot the surfaces
%draw the grand circle and equator
% continue drawing axes
node[left,rotate=90]{$x_3$};
\draw[-latex]   (X) --  (0,pi*\Radius,0) coordinate (Xend)
node[above,rotate=90]{$x_2$};
\draw[-latex]   (Y) --  (0,0,1.4*\Radius) coordinate (Yend)
node[above,rotate=90]{$x_1$};
% angle arc
\draw[-latex] let \p1=($(Xend)-(0,0,0)$),\n1={atan2(\y1,\x1)},
\p2=($0.85*(Yend)$),\n2={veclen(\y2,\x2)} in
($0.85*(Yend)$) arc(90:\n1:\n2) node[midway,above=4pt,rotate=90]{$\varphi$};
\end{axis}
\end{tikzpicture}
\end{document}


Let me remark that users who do not provide an MWE on this site sometimes have the reputation to make tons of additional requests in form of comments instead of asking separate questions (which is free after all). I hope that this prejudice does not apply to you.

• Very neat result ! – BambOo Nov 15 '18 at 18:04
• What does MWE mean? – Rainb Jan 6 '20 at 12:09