The background to this question is: I want to "zoom" into a certain area of a function and show it a little bigger. Furthermore, I want to put a gray rectangle as a background of the zoomed area, including the labels and tick marks. I use layers to achieve a background for the axis, as shown in the minimum example. Currently the rectangle is only as wide as the axis itself.
How do I get the size of the axis with labels?
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=1.7} % Set the pgf plots to a current version
\usetikzlibrary{calc,plotmarks,positioning} % For the plotting
\begin{document}
\begin{tikzpicture}
\pgfdeclarelayer{background}
\pgfdeclarelayer{inbetween}
\pgfsetlayers{background,inbetween,main}
\begin{pgfonlayer}{background}
\begin{axis}[%
name = BG,
unbounded coords=jump,
scale only axis,
xmin=-3.68158764150225, xmax=4.05456770289782,
ymin=-1.44575077919192, ymax=1.05200357048622,
axis lines*=left,
axis equal image]
\addplot [
color=blue,
solid,
mark=+,
mark options={solid},
forget plot
]
{sin(deg(x))};
\end{axis}
\end{pgfonlayer}
\begin{axis}[%
unbounded coords=jump,
at = (BG),
name = FG,
anchor = west,
xshift = 2cm,
scale only axis,
xmin=-2, xmax=-1,
ymin=-1.44575077919192, ymax=-0.5,
axis lines*=left,
axis equal image]
\addplot [
color=blue,
solid,
mark=+,
mark options={solid},
forget plot
]
{sin(deg(x))};
\end{axis}
\begin{pgfonlayer}{background}
\definecolor{inbetweengray}{cmyk}{0,0,0,0.1}
\draw[color=black,fill=inbetweengray] (FG.north west) rectangle (FG.south east);
\end{pgfonlayer}
\end{tikzpicture}%
\end{document}

spylibrary may help you: an example of usage within pgfplots is Circular drop shadow around a spy in pgfplots. – Claudio Fiandrino Mar 14 '13 at 14:18