# tikzpicture[ ] and axis[ ] do not align

I would like to combine figures drawn outside of the scope of axis but inside the scope of \tikzpicture with figures inside the \axis environment. However they seem to leave in a different reference space. I provide a minimal working example:

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{tikz-3dplot}
\usepackage{pgfplots}

\usetikzlibrary{calc,3d,shapes, pgfplots.external}

\begin{document}
\begin{tikzpicture}
\def\R{1}
\fill[ball color=white!10] (0,0) circle (\R); % 3D lighting effect
\begin{axis}[axis lines=none,
samples=50,domain=0:360,y domain=0:90,
xmin=-1.2,xmax=1.2,ymin=-1.2,ymax=1.2,zmin=0,zmax=1.2]
\pgfplotsset{
colormap={blackwhite}{gray(0cm)=(0); gray(1cm)=(1)}
}
({cos(x)*cos(y)}, {sin(x)*cos(y)}, {sin(y)});
\end{axis}
\end{tikzpicture}

\end{document}


The figure is next:

Since I have not said anything about origin and scale, the two coordinate systems should be on synch but they have different origin and different scale. Why?

Thanks.

First, the coordinate system in a pgfplots axis is completely separate from that of the surrounding tikzpicture. Remember that an axis has a default width (240pt), and it will have that size regardless of the range. For example, if you have two axis environments, one with xmin=0,xmax=1 and one with xmin=0,xmax=10, they will still have the same width. Thus, 1 unit length in the first axis will be 10 times longer than in the second axis, when printed on paper.

Second, I think this may get complicated by the fact that you're mixing 2D and 3D. Consider this image, where the axis box is shown:

That the height ended being "correct" in the code below was just luck I think. The unit vectors in the axis, in a 2D reference system, will depend on the view, width and axis limits, so how to get the correct width is not obvious to me.

An axis will always have its own coordinate system, I'm not sure why you expect them to be the same. The axis is placed with its south west corner (the default anchor) in (0,0) by default. If you add name=theAx to the axis options, and add e.g. \fill (theAx.south west) circle[radius=3pt]; you will see that this is placed in the center of your shaded circle.

If you want the origin of the axis to be in the center of the circle, add anchor=origin to the axis options. To get about the same scale, you will need to make sure that 1 unit in the axis coordinate system is the same as 1 unit (by default, 1cm) in the tikzpicture coordinate system. I guess that with your settings of xmin and xmax, adding scale only axis,width=2.4cm will make it about right. Scaling is more complicated as explained above, and I don't really have a good solution for that.

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}
\def\R{1}
\fill[ball color=white!10] (0,0) circle (\R); % 3D lighting effect
\begin{axis}[axis lines=none,name=theAx,anchor=origin,
samples=10,domain=0:360,y domain=0:90,scale only axis,width=2.4cm,
xmin=-1.2,xmax=1.2,ymin=-1.2,ymax=1.2,zmin=0,zmax=1.2]
\pgfplotsset{
colormap={blackwhite}{gray(0cm)=(0); gray(1cm)=(1)}
}

• @HermanJaramillo What I said above was a bit wrong, will see later if I can figure out a better solution. And no, you shouldn't expect that a scope will help. – Torbjørn T. Nov 15 '15 at 16:03