[Tex/LaTex] How to zoom a rectangle area in pgfplots adding a gray background even for the axis labels

pgfplotsspytikz-pgfzooming

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}

Best Answer

Using the same method provided by @percusse in Circular drop shadow around a spy in pgfplots, it is possible to define a new spy style able to fill in gray the zoomed area:

\tikzset{new spy style/.style={spy scope={%
 magnification=5,
 size=1.25cm, 
 connect spies,
 every spy on node/.style={
   rectangle,
   draw,
   },
 every spy in node/.style={
   draw,
   rectangle,
   fill=gray!40,
   }
  }
 }
}

The style by default has some magnification (meaning size of the area to be zoomed) and size (meaning size of the zoomed gray area) values which can be overridden locally with the options of \spy.

An example:

\documentclass[tikz,border=2pt,png]{standalone}

\usepackage{pgfplots}
%\usepackage{tikz}% no needs since pgfplots loads already it
\pgfplotsset{compat=1.7} % Set the pgf plots to a current version
\usetikzlibrary{spy}

\begin{document}
\tikzset{new spy style/.style={spy scope={%
 magnification=5,
 size=1.25cm, 
 connect spies,
 every spy on node/.style={
   rectangle,
   draw,
   },
 every spy in node/.style={
   draw,
   rectangle,
   fill=gray!40,
   }
  }
 }
} 
\begin{tikzpicture}[new spy style]
\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},
    ]
    {sin(deg(x))};     
\end{axis}
\spy on (5.835,2.65) in node at (2.5,3);
\spy[size=2cm,magnification=3] on (6.2,-0.15) in node at (6,-3);
\end{tikzpicture}%
\end{document}

The result:

enter image description here