1

I am having trouble using \spy on a scaled tikzpicture. Example code:

\documentclass[border=0.5mm]{standalone}
\usepackage{tikz}
%\usepackage{tikzscale}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{spy,backgrounds}

\begin{document}
\begin{tikzpicture}[scale=0.75, every node/.style={scale=0.75},
    spy using outlines={circle, magnification=3, connect spies},
    ]
    \begin{axis}
        \addplot+[thick, domain=-10:10, samples=20] {10*x^7};
        \spy[black,size=2cm] on (1.4,2) in node [fill=white] at (4,1.5);
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

There are two things I want to change:

  1. The region of the plot being magnified and the output of that magnification doesn't account for the scale of the tikzpicture (see output above). The culprit seems to be every node/.style={scale=0.75}. I tried using \scalebox{0.75}{...} around the whole tikzpicture, but it doesn't seem to change anything, nor does putting spy in a separate scope. This post seems relevant although the tikzpicture isn't been scaled in that example.

  2. If possible, I want the magnified region to show the curve 10x^7 without marks:

    \addplot+[no marks, thick, domain=-10:10, samples=20] {10*x^7};
    

while the main panel should display the same curve with marks:

    \addplot+[thick, domain=-10:10, samples=20] {10*x^7};

I have tried fiddling around with scope and pgfonlayer, but I can't seem to achieve something like the above. No matter the ordering of \addplot and \spy, the spy region will always be taken on the top layer it seems.

edit: My idea with scope is that I should be able to do something like

\begin{tikzpicture}[scale=0.75, every node/.style={scale=0.75},
    spy using outlines={circle, magnification=3, connect spies},
    ]
    \begin{scope}
    \begin{axis}
        \addplot+[thick, domain=-10:10, samples=20] {10*x^7};
    \end{axis}
    \end{scope}

    \begin{scope}
    \begin{axis}
        \addplot+[no marks, thick, domain=-10:10, samples=20] {10*x^7};
        \spy[black,size=2cm] on (1.4,2) in node [fill=white] at (4,1.5);
    \end{axis}
    \end{scope}
\end{tikzpicture}

There should be two figures superimposed on top of each other, with spy pointing to something different than what appears in the main panel. But the above apparently does nothing.

1 Answer 1

4

Spy uses a transform canvas on the scope that has been defined as the spy scope. The spy nodes creation is postponed till the end of the spy scope. -so you do not want to make the whole tikzpicture the spy scope.

If you really want to scale the picture, you can use transform canvas={scale=0.75}, but that creates many problems.

Here I have also created two coordinates to place the nodes using PGFPlots coordinate system:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{spy}
\begin{document}
\begin{tikzpicture}
\begin{scope}[transform canvas={scale=0.75}]
\begin{axis}
\addplot+[thick, domain=-10:10, samples=20] {10*x^7};
\end{axis}
\begin{scope}[spy using outlines={circle, magnification=3, size=2cm, connect spies}]
\begin{axis}
\addplot+[no marks, thick, domain=-10:10, samples=20] {10*x^7};
\coordinate (spyin) at (-5,0);
\coordinate (spyout) at (3,-0.5e8);
\spy on (spyin) in node[fill=white] at (spyout);
\end{axis}
\end{scope}
\end{scope}
\useasboundingbox (-1,-1) rectangle (6,5);
\end{tikzpicture}
\end{document}

Graph with a spy node

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .