# How can one draw a segment of a sphere with TikZ?

I am trying to replace the black plan with a segment of a sphere using tikz :

The sphere would be of centre H(Y_i) and radius ||H(Y_i) , H(Y_i+1^0)|| on which will lie all the points from k=0 (green point) to k (blue point) (=> corresponding to an arc of a circle in the plan designated by the axis system in the upper left corner).

Any propositions using tikz to add a part of a sphere would be greatly appreciated. Thank you.

With the help of @marmot :

Thanks again for this nice rendering.

• welcome to tex.se! to my opinion your question (as is) is to broad and type do-it-for-me, so it will probably will be closed .... please show us, what you try so far. Commented Mar 30, 2019 at 21:17
• Your own pic looks great! (In principle you could add ball shading by clipping the path and then shade a full sphere using \shade[ball color=...]....
– user121799
Commented Apr 1, 2019 at 2:58

The purpose of this post is to define a style that draws a part of a sphere. More specifically, it will draw a segment in which the latitude and longitude are in a certain range. Drawing this segment is then as simple as saying

\draw[thin,fill=white,fill opacity=0.6,
sphere segment={phi from 60 to 120 and theta from -10 to 50 and radius 4}];


where phi and theta are the longitude and latitude parameters.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d}
\begin{document}
\tdplotsetmaincoords{70}{30}
\begin{tikzpicture}[tdplot_main_coords,bullet/.style={fill,circle,inner
sep=1pt},sphere segment/.style args={%
phi from #1 to #2 and theta from #3 to #4 and radius #5}{insert path={%
plot[variable=\x,smooth,domain=#2:#1]
-- plot[variable=\x,smooth,domain=#3:#4]
--plot[variable=\x,smooth,domain=#1:#2]
-- plot[variable=\x,smooth,domain=#4:#3]
>=stealth,declare function={f(\x)=exp(-2+0.5*\x);}]
\draw[thick] (0,0,0) -- (4,0,0);
\draw[thick,orange] plot[variable=\x,smooth,domain=0:4] ({\x},0,{f(\x)});
% note: this domain is an approximation, it should really be
% from 0 to xmax where xmax is the solution of x^2+f(x)^2=4^2
\draw[thin,fill=white,fill opacity=0.6,
sphere segment={phi from 60 to 120 and theta from -10 to 50 and radius 4}] ;
\draw[thick,->] (4,0,0) -- (6,0,0);
\draw[thick,orange] plot[variable=\x,smooth,domain=4:6] ({\x},0,{f(\x)});
% should be xmax:6
\end{tikzpicture}
\end{document}


Or a version for Henri Menke, who suggested the syntax

\clip[sphere segment={r=4, phi=60:120, theta=-10:50}];


The clip is used to cut out a segment of a sphere. (A sphere is just a circle in tdplot_screen_coordinate since the orthographic projection of any sphere is a circle in the screen coordinates. Most likely I reinvented some things...)

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d}
\tikzset{3d stuff/.is family,
3d stuff/.cd,
parse domain/.code args={#1:#2}{\def\xmin{#1}\def\xmax{#2}},
sphere segment/.is family,
sphere segment/.cd,
r/.initial=1,
phi/.initial=0:30,
theta/.initial=0:30
}
\begin{document}
\tdplotsetmaincoords{70}{30}
\begin{tikzpicture}[tdplot_main_coords,bullet/.style={fill,circle,inner
sep=1pt},sphere segment/.style={
/utils/exec=\tikzset{3d stuff/sphere segment/.cd,#1}%
\pgfkeys{/tikz/3d stuff/parse domain/.expanded=\pgfkeysvalueof{/tikz/3d stuff/sphere segment/phi}}
\edef\phimin{\xmin}
\edef\phimax{\xmax}
\pgfkeys{/tikz/3d stuff/parse domain/.expanded=\pgfkeysvalueof{/tikz/3d stuff/sphere segment/theta}}
\edef\thetamin{\xmin}
\edef\thetamax{\xmax},
insert path={%
plot[variable=\x,smooth,domain=\phimax:\phimin]
longitude=\x,latitude=\thetamin)
-- plot[variable=\x,smooth,domain=\thetamin:\thetamax]
longitude=\phimin,latitude=\x)
--plot[variable=\x,smooth,domain=\phimin:\phimax]
longitude=\x,latitude=\thetamax)
-- plot[variable=\x,smooth,domain=\thetamax:\thetamin]
longitude=\phimax,latitude=\x) --cycle}},
>=stealth,declare function={f(\x)=exp(-2+0.5*\x);}]
\draw[thick] (0,0,0) -- (4,0,0);
\draw[thick,orange] plot[variable=\x,smooth,domain=0:4] ({\x},0,{f(\x)});
% note: this domain is an approximation, it should really be
% from 0 to xmax where xmax is the solution of x^2+f(x)^2=4^2
\begin{scope}
\clip[sphere segment={r=4, phi=60:120, theta=-10:50}];
\end{scope}
\draw[thick,->] (4,0,0) -- (6,0,0);
\draw[thick,orange] plot[variable=\x,smooth,domain=4:6] ({\x},0,{f(\x)});
% should be xmax:6
\end{tikzpicture}
\end{document}


• Thanks for your answer @marmot, I should have just ask it with MWE in the first time, that wasn't intended to be the whole picture but more like what you just did, my bad... Commented Mar 30, 2019 at 21:45
• @Yacola No worries and glad to hear that this goes in the right direction.
– user121799
Commented Mar 30, 2019 at 21:57
• A nicer syntax would be sphere segment={r=4, phi=60:120, theta=-10:50} in my opinion. Commented Mar 31, 2019 at 0:25
• @HenriMenke Done. Note that one does not only need the domain but also explicitly the lower and upper ends (otherwise, yes, I would just have inserted the pgfkeys...).
– user121799
Commented Mar 31, 2019 at 2:45