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: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:
The result: