# How to draw a squeezed ellipse with fading color on a sphere surface with TikZ?

Ideally, I would like to draw something like part (c) of the image below: ellipsonsphere http://quantop.nbi.ku.dk/research/nanofiber/spin_squeezing/figure6a-c.png. Here, I narrow down the question to how to draw an ellipse on the surface of a sphere with fading color as shown on part (b) of the figure. (Note: Maybe I can follow this example to draw arrows on the surface of the sphere to finish (c) from (b)).

1. The first attempt is to basically follow a simple example to draw the sphere, and found that it did not work out for the current version of TikZ. Now I stuck at this step. Here is my minimum code on finishing a part of this job for the first attempt:


\documentclass[border=2pt]{standalone} %\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathmorphing,shapes,arrows,math}
\begin{document}
\newcommand\pgfmathsinandcos3{%
\pgfmathsetmacro#1{sin(#3)}%
\pgfmathsetmacro#2{cos(#3)}%
}
\newcommand\LongitudePlane3[current plane]{%
\pgfmathsinandcos\sinEl\cosEl{#2} % elevation
\pgfmathsinandcos\sint\cost{#3} % azimuth
\tikzset{#1/.style={cm={\cost,\sint*\sinEl,0,\cosEl,(0,0)}}}
}
\newcommand\LatitudePlane3[current plane]{%
\pgfmathsinandcos\sinEl\cosEl{#2} % elevation
\pgfmathsinandcos\sint\cost{#3} % latitude
\pgfmathsetmacro\yshift{\cosEl*\sint}
\tikzset{#1/.style={cm={\cost,0,0,\cost*\sinEl,(0,\yshift)}}} %
}
\newcommand\DrawLongitudeCircle2{
\LongitudePlane{\angEl}{#2}
\tikzset{current plane/.prefix style={scale=#1}}
% angle of "visibility"
\pgfmathsetmacro\angVis{atan(sin(#2)*cos(\angEl)/sin(\angEl))} %
\draw[current plane] (\angVis:1) arc (\angVis:\angVis+180:1);
\draw[current plane,dashed] (\angVis-180:1) arc (\angVis-180:\angVis:1);
}
\newcommand\DrawLatitudeCircle2{
\LatitudePlane{\angEl}{#2}
\tikzset{current plane/.prefix style={scale=#1}}
\pgfmathsetmacro\sinVis{sin(#2)/cos(#2)*sin(\angEl)/cos(\angEl)}
% angle of "visibility"
\pgfmathsetmacro\angVis{asin(min(1,max(\sinVis,-1)))}
\draw[current plane] (\angVis:1) arc (\angVis:-\angVis-180:1);
\draw[current plane,dashed] (180-\angVis:1) arc (180-\angVis:\angVis:1);
}

%% document-wide tikz options and styles

\tikzset{%

=latex, % option for nice arrows
inner sep=0pt,%
outer sep=2pt,%
mark coordinate/.style={inner sep=0pt,outer sep=0pt,minimum size=3pt,
fill=black,circle}%
}

\begin{figure}[ht]
\begin{center}
\begin{tikzpicture} % MERC

% Modified from Stereographic and cylindrical map projections
% Author: Tomasz M. Trzeciak
% Source: LaTeX-Community.org
%         http://www.latex-community.org/viewtopic.php?f=4&t=2111

%% some definitions

\def\angEl{25} % elevation angle
\def\angAz{-100} % azimuth angle
\def\angPhiOne{-50} % longitude of point P
\def\angPhiTwo{-35} % longitude of point Q
\def\angBeta{33} % latitude of point P and Q

%% working planes

\pgfmathsetmacro\H{\R*cos(\angEl)} % distance to north pole
\LongitudePlane[xzplane]{\angEl}{\angAz}
\LongitudePlane[pzplane]{\angEl}{\angPhiOne}
\LongitudePlane[qzplane]{\angEl}{\angPhiTwo}
\LatitudePlane[equator]{\angEl}{0}

%% draw background sphere

\fill[ball color=white] (0,0) circle (\R); % 3D lighting effect
%\fill[white] (0,0) circle (\R); % just a white circle
\draw (0,0) circle (\R);

%% characteristic points

\coordinate (O) at (0,0);
\coordinate[mark coordinate] (N) at (0,\H);
\coordinate[mark coordinate] (S) at (0,-\H);
\path[xzplane] (\R,0) coordinate (XE);
\path[pzplane] (\angBeta:\R) coordinate (P);
\path[pzplane] (\R,0) coordinate (PE);
\path[qzplane] (\angBeta:\R) coordinate (Q);
\path[qzplane] (\R,0) coordinate (QE);

%% meridians and latitude circles

% \DrawLongitudeCircle[\R]{\angAz} % xzplane
% \DrawLongitudeCircle[\R]{\angAz+90} % yzplane
\DrawLongitudeCircle[\R]{\angPhiOne} % pzplane
%\DrawLongitudeCircle[\R]{\angPhiTwo} % qzplane
\DrawLatitudeCircle[\R]{\angBeta}
\DrawLatitudeCircle[\R]{0} % equator

% shifted equator in node with nested call to tikz
% (I didn't know it's possible)
%\node at (0,1.6*\R) { \tikz{\DrawLatitudeCircle[\R]{0}} };

%% draw lines and put labels

%\draw (-\R,-\H) -- (-\R,2*\R) (\R,-\H) -- (\R,2*\R);
%\draw[->] (O) -- +(0,1.5*\R) node[above] {$|0\rangle$};
\node[above=8pt] at (N) {$|0\rangle$};%{$\mathbf{N}$};
\node[below=8pt] at (S) {$|1\rangle$};%{$\mathbf{S}$};
\node[right=8pt] at (P) {$|\Phi \rangle$};
\draw[->] (O) -- (P);
\draw[dashed] (O) -- (N);
\draw[dashed] (XE) -- (O) -- (PE);
%\draw[dashed] (O) -- (QE);
%\draw[pzplane,->,thin] (0:0.5*\R) to[bend right=15]
%    node[midway,right] {$\beta$} (\angBeta:0.5*\R);
\path[pzplane] (0.5*\angBeta:\R) ;%node[right] {$\hat{1}$};
\path[qzplane] (0.5*\angBeta:\R) ;%node[right] {$\hat{2}$};
\draw[equator,->,thin] (\angAz:0.5*\R) to[bend right=30]
node[pos=0.4,above] {$\phi$} (\angPhiOne:0.5*\R);
\draw[pzplane,->,thin] (90:0.5*\R) to[bend left=30]
node[midway,right] {$\theta$} (\angBeta:0.5*\R);
\draw[equator,->] (-90:\R) arc (-90:-70:\R) ;%node[below=0.3ex] {$x = a\phi$};
\path[xzplane] (0:\R) node[below] {$\theta=0$};
\path[xzplane] (\angBeta:\R);% node[below left] {$\beta=\beta_0$};

\end{tikzpicture}
\end{center}
\caption{Ellipse on a Sphere}
\label{fig:sphere}
\end{figure}
\end{document}


Preview: As you can see, compared with the original example (the lower-left plot), the two dashed lines on the equator cannot show up in my modified version. This error occurs even when I compile the code of the original example. Noticed that I have replaced estyle to style. Most importantly, what should I do next to make the ellipse and fading colors on the sphere at an arbitrary position? Thanks again.

1. The second attempt is to use 3dplot to finish the sphere, and the code is very simple (with imperfections). Below is the code:

\documentclass[11pt]{standalone}
%\usepackage[utf8]{inputenc}
\usepackage{tikz,tikz-3dplot}
\usetikzlibrary{arrows}
\begin{document}
\tdplotsetmaincoords{70}{135}
\begin{tikzpicture}
\begin{scope}[tdplot_main_coords, fill opacity=.7,>=latex,shape=circle]
\pgfsetlinewidth{.1pt}]
%draw sphere
\tdplotsphericalsurfaceplot{36}{12}{1}{black!85!white}{blue!40!white}
{\draw[color=black,thick,->]  (-1.5,0,0) --  (1.5,0,0)  node[anchor=north  east]{$x$};}
{\draw[color=black,thick,->]  (0,-1.5,0) --  (0,1.5,0)  node[anchor=north  west]{$y$};}
{\draw[color=black,thick,->]  (0,0,-1.5) --  (0,0,1.5)  node[anchor=south]{$z$};}
\end{scope}
\end{tikzpicture}
\end{document}

and the preview: I don't know how to do the ellipse next. Thoughts?

• Why did you replace estyle by style? – cfr Jan 14 '15 at 15:36
• @cfr This is due to the version change of TikZ. See here to correctly show the longitude and latitude lines. But the dashed lines from the origin to the equator are still not showing up in my case. – Xiaodong Qi Jan 14 '15 at 16:50
• Ah, thanks. Just I knew that estyle was still supported (according to the documentation) and so couldn't figure out why you'd change it. A bug explains that! – cfr Jan 14 '15 at 17:34

Here an alternative to get to the desired image. I did just an approximation (no mathematical correct drawing) on the path on the globe. See this as a simple how to.

\documentclass[tikz, border=6mm]{standalone}

\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[x={(215:.75cm)},y={(-10:1cm)},z={(90:1cm)}]

% Axes
\begin{scope}[black!50, >=latex, ->, font=\scriptsize]
\draw (0,0,0) -- ++(1.5,0,0) coordinate [label={0:$x$}];
\draw (0,0,0) -- ++(0,1.5,0) coordinate [label={35:$y$}];
\draw (0,0,0) -- ++(0,0,1.5) coordinate [label={90:$z$}];
\end{scope}

% Globe
\begin{scope}[blue!50!white, samples=250, domain=0:2*pi]
\draw plot ({cos(\x r)}, {sin(\x r)}, 0); % xy
\draw plot ({cos(\x r)}, 0, {sin(\x r)}); % xz
\draw plot (0, {cos(\x r)}, {sin(\x r)}); % yz
\fill [opacity=.5] (0,0) circle (.85cm);
\end{scope}

% Plot around globe - just approximation!
\begin{scope}[smooth, red, >=latex, ->, scale=.85]
\draw [domain=0:.875] plot ({cos(\x r)},{sin(\x r)},0);
\draw [domain=.19:pi/3.5] plot ({cos(\x r)},{cos(\x r)},{sin(\x r)}) coordinate (c-x);
\end{scope}

\begin{scope}[on background layer, smooth]
% projection area with plot
\fill [blue!50!white] (-4,-1.5,-1.5) -- (-4,1.5,-1.5) -- (-4,1.5,1.5) -- (-4,-1.5,1.5);
\draw [black!25, very thin] (-4,0,-1.5) -- (-4,0,1.5);
\draw [black!25, very thin] (-4,-1.5,0) -- (-4,1.5,0);
\draw [black!75, domain=-1.5:1.5] plot (-4,{\x},{sin((2*\x) r)});
\draw [black!25, domain=-1.5:1.5] plot (-4,{\x},{0.8*sin((2*\x) r)});

% grey filled circle
\fill [black!15, opacity=.5] (0,0,0) circle (1.155cm);

% projection of globe-coordinate
\draw [dashed] (c-x) -- +(-4,0,0);
\end{scope}
\end{tikzpicture}
\end{document} • This looks good! But how to draw the projected ellipse on the surface of the sphere? – Xiaodong Qi Jul 20 '15 at 21:31