# 3D Surface Plot

I am using TIKZ (tikz-3d) plot to create a cartesian grid and to create a surface(a section of a sphere) passing through the surface.

First I create a 3d cartesian mesh using the following code:-

\usepackage{pgf,tikz}
\usepackage{pgfplots}
\usepackage{tikz-3dplot}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{empty}

\begin{document}

\newcommand{\csize}{2}

\tdplotsetmaincoords{75}{105}
\begin{tikzpicture}
[tdplot_main_coords,
grid/.style={very thin,gray},
axis/.style={->,blue,very thick},
cube/.style={opacity=.5,very thick,fill=red},
ccube/.style={opacity=.1,thin,fill=yellow!20!white},
plane/.style={opacity=.5,draw=none,fill=blue!20!white},
line/.style={very thick}]

% bottom plane
\draw[plane] (-0.5,-0.5,0) -- (7.5,-0.5,0) -- (7.5,7.5,0) -- (-0.5,7.5,0) -- cycle;

%draw the axes
\draw[axis] (0,0,0) -- (8,0,0) node[anchor=west]{$y$};
\draw[axis] (0,0,0) -- (0,8,0) node[anchor=west]{$x$};
\draw[axis] (0,0,0) -- (0,0,7) node[anchor=west]{$z$};

\foreach \x in {0,2,4}
\foreach \y in {0,2,4}
\foreach \z in {0,2,4}
{
\ifthenelse{\x = 2 \AND \y = 2 \AND \z = 2}
{
\coordinate (O) at (2,2,2);
%draw the bottom of the cube
\draw[cube] (O) -- ($(O) + (0,\csize,0)$) -- ($(O) + (\csize,\csize,0)$) -- ($(O) + (\csize,0,0)$) -- cycle;
\draw[cube] (O) -- ($(O) + (0,\csize,0)$) -- ($(O) + (0,\csize,\csize)$) -- ($(O) + (0,0,\csize)$) -- cycle;
\draw[cube] (O) -- ($(O) + (\csize,0,0)$) -- ($(O) + (\csize,0,\csize)$) -- ($(O) + (0,0,\csize)$) -- cycle;
\draw[cube] ($(O) + (\csize,0,0)$) -- ($(O) + (\csize,\csize,0)$) -- ($(O) + (\csize,\csize,\csize)$) -- ($(O) + (\csize,0,\csize)$) -- cycle;
\draw[cube] ($(O) + (0,\csize,0)$) -- ($(O) + (\csize,\csize,0)$) -- ($(O) + (\csize,\csize,\csize)$) -- ($(O) + (0,\csize,\csize)$) -- cycle;
\draw[cube] ($(O) + (0,0,\csize)$) -- ($(O) + (0,\csize,\csize)$) -- ($(O) + (\csize,\csize,\csize)$) -- ($(O) + (\csize,0,\csize)$) -- cycle;
}
{
\coordinate (O) at (\x,\y,\z);
%draw the bottom of the cube
\draw[ccube] (O) -- ($(O) + (0,\csize,0)$) -- ($(O) + (\csize,\csize,0)$) -- ($(O) + (\csize,0,0)$) -- cycle;
\draw[ccube] (O) -- ($(O) + (0,\csize,0)$) -- ($(O) + (0,\csize,\csize)$) -- ($(O) + (0,0,\csize)$) -- cycle;
\draw[ccube] (O) -- ($(O) + (\csize,0,0)$) -- ($(O) + (\csize,0,\csize)$) -- ($(O) + (0,0,\csize)$) -- cycle;
\draw[ccube] ($(O) + (\csize,0,0)$) -- ($(O) + (\csize,\csize,0)$) -- ($(O) + (\csize,\csize,\csize)$) -- ($(O) + (\csize,0,\csize)$) -- cycle;
\draw[ccube] ($(O) + (0,\csize,0)$) -- ($(O) + (\csize,\csize,0)$) -- ($(O) + (\csize,\csize,\csize)$) -- ($(O) + (0,\csize,\csize)$) -- cycle;
\draw[ccube] ($(O) + (0,0,\csize)$) -- ($(O) + (0,\csize,\csize)$) -- ($(O) + (\csize,\csize,\csize)$) -- ($(O) + (\csize,0,\csize)$) -- cycle;
}
}

\end{tikzpicture}

\end{document}


Which leads to the following figure :-

Now, I go on to add a spherical surface by making use of the function \tdplotsphericalsurfaceplot using the following block of code placed just before creating the cartesian grids.

\tdplotsetpolarplotrange{0}{90}{0}{180}
\tdplotsphericalsurfaceplot[opacity=.2]{72}{36}{5}{blue!20!white}{blue!20!white}%
{\draw[color=black,thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};}% just for debugging
{\draw[color=black,thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};}% just for debugging
{\draw[color=black,thick,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};}% just for debugging


My question is how to move the centre of the sphere to a far off distance and make the radius large enough (i can change the radius by changing 5 to some big number) so that only a section of the surface passes through the red cube (and associated neighbors). In essence how to change the origin of the sphere keeping all other things same.

Welcome to TeX.SE! It is absolutely straightforward to move the origin of the sphere around: just put it into a scope \begin{scope}[shift={(<xshift>,<yshift>,<zshift>)}] ... \end{scope}.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{arrows}
\begin{document}

\newcommand{\csize}{2}
\tdplotsetmaincoords{75}{105}
\begin{tikzpicture}
[tdplot_main_coords,
grid/.style={very thin,gray},
axis/.style={->,blue,very thick},
cube/.style={opacity=.5,very thick,fill=red},
ccube/.style={opacity=.1,thin,fill=yellow!20!white},
plane/.style={opacity=.5,draw=none,fill=blue!20!white},
line/.style={very thick}]

% bottom plane
\draw[plane] (-0.5,-0.5,0) -- (7.5,-0.5,0) -- (7.5,7.5,0) -- (-0.5,7.5,0) -- cycle;

%draw the axes
\draw[axis] (0,0,0) -- (8,0,0) node[anchor=west]{$y$};
\draw[axis] (0,0,0) -- (0,8,0) node[anchor=west]{$x$};
\draw[axis] (0,0,0) -- (0,0,7) node[anchor=west]{$z$};

\foreach \x in {0,2,4}
\foreach \y in {0,2,4}
\foreach \z in {0,2,4}
{
\ifthenelse{\x = 2 \AND \y = 2 \AND \z = 2}
{
\coordinate (O) at (2,2,2);
%draw the bottom of the cube
\draw[cube] (O) -- ($(O) + (0,\csize,0)$) -- ($(O) + (\csize,\csize,0)$) -- ($(O) + (\csize,0,0)$) -- cycle;
\draw[cube] (O) -- ($(O) + (0,\csize,0)$) -- ($(O) + (0,\csize,\csize)$) -- ($(O) + (0,0,\csize)$) -- cycle;
\draw[cube] (O) -- ($(O) + (\csize,0,0)$) -- ($(O) + (\csize,0,\csize)$) -- ($(O) + (0,0,\csize)$) -- cycle;
\draw[cube] ($(O) + (\csize,0,0)$) -- ($(O) + (\csize,\csize,0)$) -- ($(O) + (\csize,\csize,\csize)$) -- ($(O) + (\csize,0,\csize)$) -- cycle;
\draw[cube] ($(O) + (0,\csize,0)$) -- ($(O) + (\csize,\csize,0)$) -- ($(O) + (\csize,\csize,\csize)$) -- ($(O) + (0,\csize,\csize)$) -- cycle;
\draw[cube] ($(O) + (0,0,\csize)$) -- ($(O) + (0,\csize,\csize)$) -- ($(O) + (\csize,\csize,\csize)$) -- ($(O) + (\csize,0,\csize)$) -- cycle;
}
{
\coordinate (O) at (\x,\y,\z);
%draw the bottom of the cube
\draw[ccube] (O) -- ($(O) + (0,\csize,0)$) -- ($(O) + (\csize,\csize,0)$) -- ($(O) + (\csize,0,0)$) -- cycle;
\draw[ccube] (O) -- ($(O) + (0,\csize,0)$) -- ($(O) + (0,\csize,\csize)$) -- ($(O) + (0,0,\csize)$) -- cycle;
\draw[ccube] (O) -- ($(O) + (\csize,0,0)$) -- ($(O) + (\csize,0,\csize)$) -- ($(O) + (0,0,\csize)$) -- cycle;
\draw[ccube] ($(O) + (\csize,0,0)$) -- ($(O) + (\csize,\csize,0)$) -- ($(O) + (\csize,\csize,\csize)$) -- ($(O) + (\csize,0,\csize)$) -- cycle;
\draw[ccube] ($(O) + (0,\csize,0)$) -- ($(O) + (\csize,\csize,0)$) -- ($(O) + (\csize,\csize,\csize)$) -- ($(O) + (0,\csize,\csize)$) -- cycle;
\draw[ccube] ($(O) + (0,0,\csize)$) -- ($(O) + (0,\csize,\csize)$) -- ($(O) + (\csize,\csize,\csize)$) -- ($(O) + (\csize,0,\csize)$) -- cycle;
}
}
\begin{scope}[shift={(0,-4,0)},opacity=0.2]
\tdplotsetpolarplotrange{0}{90}{00}{180}
\tdplotsphericalsurfaceplot{72}{36}{6}{blue!20!white}{blue!20!white}%
{\draw[color=black,thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};}% just for debugging
{\draw[color=black,thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};}% just for debugging
{\draw[color=black,thick,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};}% just for debugging
\end{scope}

\end{tikzpicture}
\end{document}


Note, however, that I was not able to reproduce the screen shot you provide with the commands you disclose. How do achieve that? Of course, the center of the sphere got shifted here.

• its the sequence that you have altered, if you first plot the surface and then the grid you arrive at the snapshot that I have provided :) Thanks for the reply. If i have to only show the grid lines of the spherical surface at the ends and not the in between theta and phi then how to proceed ? – datapanda Nov 17 '18 at 22:50
• @datapanda Could you please provide a complete code that starts with \documentclass and ends with \end{document}? (Note that the packages \usepackage{pgfplots} and \usepackage{mathrsfs} are not used here.) And then please try to reword your last statement, in its present form I can't understand it. – marmot Nov 17 '18 at 22:56