# plot within a plot: zoom into a plot so that the magnified part is framed by axis with ticks and tick labels

I want to zoom into a plot much like it is possible by using the spy library but I want the spy-in node to contain its own ticks and tick labels with a finer grid than the original plot. To clarify what I mean I prepared the following picture by editing a picture that was produced with pgfplots and the spy library with the program Inkscape:

Here is the code for the "pgfplots and spy"-part of the picture:

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{units}
\usetikzlibrary{spy,backgrounds}
\usepackage{pgfplotstable}

0.0    1.0
0.0   -0.5
}\datatable

\begin{document}

\begin{tikzpicture} [spy using outlines={rectangle, magnification=2, size=0.5cm, connect spies}]
\begin{axis}[
xlabel={Energy},
ylabel={Intensity},
x unit={eV}
]
\addplot +[mark=none] table [x index=0, y index=1] {\datatable};

\begin{scope}
\spy[black,size=2cm] on (4.25,1.65) in node [fill=none] at (5,5.5);
\end{scope}

\end{axis}
\end{tikzpicture}

\end{document}


I'm not sure whether this is possible using the spy library or not. Maybe another approach is needed to achieve this.

-

You need another approach to achieve the effect since a spy glass will never generate its own pgfplots axis (it operates on a lower left of pgf).

A possible solution would be to create a separate pgfplots axis into some node (perhaps a pin) and reconfigure the separate axis accordingly:

\documentclass{article}

\usepackage{pgfplots}
\usepgfplotslibrary{units}
\usetikzlibrary{spy,backgrounds}
\usepackage{pgfplotstable}

0.0    1.0
0.0   -0.5
}\datatable

\begin{document}

\begin{tikzpicture}[every pin/.style={fill=white}]
\begin{axis}[
xlabel={Energy},
ylabel={Intensity},
x unit={eV}
]
\addplot +[mark=none] table [x index=0, y index=1] {\datatable};

\coordinate (pt) at (axis cs:0,0);
\end{axis}

\node[pin=70:{%
\begin{tikzpicture}[baseline,trim axis left,trim axis right]
\begin{axis}[
tiny,
xlabel={Energy},
ylabel={Intensity},
x unit={eV},
xmin=-1,xmax=1,
ymin=-0.2,ymax=0.2,
enlargelimits,
]
\addplot +[mark=none] table [x index=0, y index=1] {\datatable};
\end{axis}
\end{tikzpicture}%
}] at (pt) {};
\end{tikzpicture}

\end{document}


This generates a separate axis inside of a pin argument. In order to reduce the bounding box (and therefore the target location of the pin), I chose to trim the axis on the left and on the right. The every pin option of the outer picture configures a fill color for the nested pin. Note that pgfplots does not support nested axes which is why I moved the pin after \end{axis} of the first plot.

If your application allows it, you could also adjust the domain argument for the inner axis in order to increase the accuracy.

I think you could also generate some rectangular border around the "magnified" solution. If it needs to be precise, it would need to be done manually using \draw ... rectangle ...;. An imprecise rectangle could be achieved using \node[rectangle,draw=black,text width=...,pin=.....].

-
Thanks. It's a very nice solution. May I ask whether it is possible to let the pin line end a little bit closer to the "magnified" solution's axis? –  Philipp Jan 28 '12 at 16:40
The pin line is always near some point of the boundary. To adjust its location, we can either modify the boundary (for example using xlabel style={overlay}) or we can change the angle in which the pin points to its node (like pin=180:...). –  Christian Feuersänger Jan 28 '12 at 18:29