Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

enter image description here

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

\documentclass{standalone}   

\usepackage{pgfplots}   
\pgfplotsset{width=10cm,compat=newest}
\usepgfplotslibrary{units}
\usetikzlibrary{spy,backgrounds}
\usepackage{pgfplotstable}

\pgfplotstableread{
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};
\addplot +[mark=none] {0.1*x^2};

\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.

share|improve this question

1 Answer 1

up vote 14 down vote accepted

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}   
\pgfplotsset{width=10cm,compat=newest}
\usepgfplotslibrary{units}
\usetikzlibrary{spy,backgrounds}
\usepackage{pgfplotstable}

\pgfplotstableread{
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};
\addplot +[mark=none] {0.1*x^2};

\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};
    \addplot +[mark=none] {0.1*x^2};
    \end{axis}
    \end{tikzpicture}%
}] at (pt) {};
\end{tikzpicture}

\end{document}

enter image description here

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=.....].

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.