# Spy glasses in a group plot

I have a group plot in which the graphs have common x axis and are stacked one over the other- like the one givenhere.

Now, I want to have a spy glass on each of these plots in the group. But when I include a spy glass in the group plot, the spy glass is plotted only on the last plot in the group. See the picture below The different circles you see in the last plot in the group are spy glasses of different size I have used for each plot, but all ended up in the last plot. How do I rectify this? Here is a MWE:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{spy}
\begin{document}
\begin{tikzpicture}[spy using outlines={circle, magnification=1.75, connect spies}]
\begin{groupplot}[
group style={
group size=1 by 4,  % sets number of columns and rows in groupplot array
vertical sep=0pt,   % vertical distance between axes
},
axis y line=left, % y axis line on left side only
xmin=0,xmax=10,   % set axis
ymin=0,           % limits
domain=1:9,       % domain, just for example
width=10cm,       % width
height=3cm,       % and height for each axis
scale only axis,  % disregard labels and ticks for scaling
no markers,
enlarge y limits=upper,
]

\nextgroupplot[
ylabel=$y$,
ylabel style={at={(rel axis cs:0,1)},above,rotate=-90}, %move ylabel a bit
axis x line=none] % remove x-axis lines
\coordinate (spypoint) at (axis cs:5,0.85);
\coordinate (magnifyglass) at (axis cs:7.5,1.3);
\spy [blue, size=2.5cm] on (spypoint) in node[fill=white] at (magnifyglass);

\nextgroupplot[axis x line=none]
\coordinate (spypoint) at (axis cs:5,0.85);
\coordinate (magnifyglass) at (axis cs:7.5,1.30);
\spy [blue, size=3.0cm] on (spypoint) in node[fill=white] at (magnifyglass);

\nextgroupplot[axis x line=none]
\coordinate (spypoint) at (axis cs:5,0.85);
\coordinate (magnifyglass) at (axis cs:7.5,1.30);
\spy [blue, size=3.5cm] on (spypoint) in node[fill=white] at (magnifyglass);

\nextgroupplot[
axis x line=bottom, % only x axis line at bottom
xlabel=$x$,
xlabel style={at={(rel axis cs:1,0)},right}]
\coordinate (spypoint) at (axis cs:5,0.85);
\coordinate (magnifyglass) at (axis cs:7.5,1.30);
\spy [blue, size=1.5cm] on (spypoint) in node[fill=white] at (magnifyglass);

\end{groupplot}
\end{tikzpicture}
\end{document}


Someone please shed some light on this.

• Why using groupplots? I don't think it's the most appropriate choice here. – Alenanno May 25 '16 at 14:57
• @Alenanno The graph given here is for representation purpose only. It is an example as in "Minimum working example" . I have a huuuuge data set which exactly requires this kind of format of plot. – Rhinocerotidae May 25 '16 at 15:00
• Try to use different names for each couple of coordinates: spypoint1 and magnifyglass1, then spypoint2 and magnifyglass1, etc. – Paul Gaborit May 25 '16 at 15:09

Try to use different names for each couple of coordinates: spypoint1 and magnifyglass1, then spypoint2 and magnifyglass2, etc.

Result:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{spy}
\begin{document}
\begin{tikzpicture}[spy using outlines={circle, magnification=1.75, connect spies}]
\begin{groupplot}[
group style={
group size=1 by 4,  % sets number of columns and rows in groupplot array
vertical sep=0pt,   % vertical distance between axes
},
axis y line=left, % y axis line on left side only
xmin=0,xmax=10,   % set axis
ymin=0,           % limits
domain=1:9,       % domain, just for example
width=10cm,       % width
height=3cm,       % and height for each axis
scale only axis,  % disregard labels and ticks for scaling
no markers,
enlarge y limits=upper,
]

\nextgroupplot[
ylabel=$y$,
ylabel style={at={(rel axis cs:0,1)},above,rotate=-90}, %move ylabel a bit
axis x line=none] % remove x-axis lines
\coordinate (spypoint1) at (axis cs:5,0.85);
\coordinate (magnifyglass1) at (axis cs:7.5,1.3);
\spy [blue, size=2.5cm] on (spypoint1) in node[fill=white] at (magnifyglass1);

\nextgroupplot[axis x line=none]
\coordinate (spypoint2) at (axis cs:5,0.85);
\coordinate (magnifyglass2) at (axis cs:7.5,1.30);
\spy [orange, size=3.0cm] on (spypoint2) in node[fill=white] at (magnifyglass2);

\nextgroupplot[axis x line=none]
\coordinate (spypoint3) at (axis cs:5,0.85);
\coordinate (magnifyglass3) at (axis cs:7.5,1.30);
\spy [green, size=3.5cm] on (spypoint3) in node[fill=white] at (magnifyglass3);

\nextgroupplot[
axis x line=bottom, % only x axis line at bottom
xlabel=$x$,
xlabel style={at={(rel axis cs:1,0)},right}]