5

I'm trying to copy the behaviour of this question: Using tikzlibrary spy without magnifying line width and/or mark size. I would like to change the shape of the spy to rectangle, but I'm failing at the scope (which I have to admin that I don't really understand why works in the original question). Could someone help me out?

The MWE:

\documentclass{memoir}
\usepackage{amsmath, tikz, pgfplots}
\usetikzlibrary{calc,spy,shapes}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
% [spy using outlines={rectangle,black,magnification=3,connect spies}]
\begin{axis}[%
width=6.01828521434821in,
height=4.74667979002625in,
unbounded coords=jump,
scale only axis,
xmin=1.25,
xmax=2,
xlabel={Robustness},
ymin=0,
ymax=20,
ylabel={Controller},
title={Case 11},
% legend style={draw=black,fill=white,legend cell align=left}
]
\newcommand*\myplots[1][]{
\addplot [
color=blue,
dashed
]
table[row sep=crcr]{
1.250000000054 0.218471486504557\\1.27500000012472 0.235880794037559\\1.30000000004985 0.253093675278193\\1.32499999988962 0.269996702493353\\1.34999999996296 0.28660169532495\\1.37500000003271 0.302803529131698\\1.4 0.318627316831094\\1.42500000000018 0.333967811176506\\1.44999999975398 0.348915976282584\\1.47499999999985 0.363393665978672\\1.50000000000007 0.377386190083728\\1.52499999999677 0.391065980885588\\1.54999999999987 0.404406696565785\\1.5749999999932 0.417357153126701\\1.60000000000001 0.429890180101467\\1.62500000007965 0.441495914781593\\1.64999999998047 0.453420186117445\\1.67500000000006 0.464885624062778\\1.70000000000013 0.475698022605379\\1.72499999999977 0.486001302461843\\1.75000000007258 0.49588760557761\\1.77500000001522 0.505406921339204\\1.79999999999992 0.514575948858101\\1.8249999999994 0.523580224222934\\1.84999999999966 0.532346980549898\\1.87499999998767 0.540884368439134\\1.90000000002483 0.549236128880721\\1.92499999997271 0.557390601810701\\1.94999999999657 0.565349329095157\\1.97499999999941 0.573100654346336\\1.99999999999993 0.580661122004988\\2.02499999998678 0.588035048696743\\2.0499999999709 0.595221272967935\\2.07499999999558 0.602215073143855\\2.09999999981776 0.609014392276178\\2.12500000001126 0.615634705868222\\2.14999999999957 0.622107376143054\\2.17499999994578 0.628535289209435\\2.19999999832383 0.63479954406909\\2.225 0.640975653060916\\2.24999999999955 0.647023272731091\\2.27499999999981 0.652900079435489\\2.29999999999833 0.658608487562972\\2.32499999999118 0.664162521737991\\2.34999999999773 0.669564611132752\\2.3750000000001 0.674786909878852\\2.40000000000024 0.679871708587361\\2.42499999998267 0.68481930558268\\2.44999999993369 0.689620895991369\\2.475 0.694300387671984\\2.50000000000226 0.698847019454327\\2.52499999999607 0.703231052229968\\2.54999999999282 0.707335949070966\\2.575 0.711299740037372\\2.59999999997469 0.715121564545835\\2.62500000002107 0.719030281788718\\2.64999999998823 0.722750679025289\\2.67499999999833 0.726355404984384\\2.6999999999989 0.729841563597055\\2.72500000000001 0.733213147420525\\2.75000000000046 0.736469697155365\\2.77500000011202 0.739681867953894\\2.8 0.742786117829059\\2.82500000000028 0.745831867778158\\2.84999999999055 0.748760245235199\\2.87499999999654 0.751587709302175\\2.90000000000011 0.754337713416536\\2.92500000001402 0.756957274828441\\2.94999999995396 0.759357524849041\\2.97499999999574 0.761687855897812\\3.00000000000154 0.764338301992918\\};
% \addlegendentry{po:pid};

\addplot [
color=green!50!black,
dashed
]
table[row sep=crcr]{
1.250000000054 0.590067068740046\\1.27500000012472 0.587559925194454\\1.30000000004985 0.586376714983635\\1.32499999988962 0.586069496190726\\1.34999999996296 0.586583764906821\\1.37500000003271 0.587586732327579\\1.4 0.589106686232638\\1.42500000000018 0.590799582313491\\1.44999999975398 0.592894933769734\\1.47499999999985 0.595115212158793\\1.50000000000007 0.597338496940835\\1.52499999999677 0.600163078036215\\1.54999999999987 0.603553183039573\\1.5749999999932 0.607355964366702\\1.60000000000001 0.611472392727962\\1.62500000007965 0.612640770551816\\1.64999999998047 0.618542970092304\\1.67500000000006 0.625478240488116\\1.70000000000013 0.63111283078678\\1.72499999999977 0.635924446858566\\1.75000000007258 0.640430875313834\\1.77500000001522 0.644633876853955\\1.79999999999992 0.647757322657836\\1.8249999999994 0.647552686193448\\1.84999999999966 0.647363620909126\\1.87499999998767 0.647117668558105\\1.90000000002483 0.646013166144435\\1.92499999997271 0.644422071010403\\1.94999999999657 0.642591297950079\\1.97499999999941 0.640866907916906\\1.99999999999993 0.639224831229371\\2.02499999998678 0.636685323281278\\2.0499999999709 0.633964341182746\\2.07499999999558 0.631446791852552\\2.09999999981776 0.628537387562625\\2.12500000001126 0.626107411465097\\2.14999999999957 0.624103000469\\2.17499999994578 0.62449136318196\\2.19999999832383 0.624449479840847\\2.225 0.6256877319412\\2.24999999999955 0.628088304645751\\2.27499999999981 0.630472877532383\\2.29999999999833 0.63283206246448\\2.32499999999118 0.635116015919806\\2.34999999999773 0.637322260692226\\2.3750000000001 0.640065236430338\\2.40000000000024 0.642534886174564\\2.42499999998267 0.644944092104574\\2.44999999993369 0.647313968075354\\2.475 0.649603381645786\\2.50000000000226 0.651905320157395\\2.52499999999607 0.654572686699921\\2.54999999999282 0.658065576936871\\2.575 0.661530686251933\\2.59999999997469 0.664985448106105\\2.62500000002107 0.667216476217143\\2.64999999998823 0.669895462979295\\2.67499999999833 0.672557424400875\\2.6999999999989 0.675234842685167\\2.72500000000001 0.677932291475736\\2.75000000000046 0.680659050710123\\2.77500000011202 0.683201448501821\\2.8 0.685708044714863\\2.82500000000028 0.688088365642656\\2.84999999999055 0.690571448381062\\2.87499999999654 0.693076214461211\\2.90000000000011 0.695575684929241\\2.92500000001402 0.698184293706798\\2.94999999995396 0.701235761040505\\2.97499999999574 0.704202908157281\\3.00000000000154 0.705980592056065\\};
}
\newcommand*\spyfactor{3.1622776601683793319988935444327}
\newcommand*\spypoint{axis cs:1.4,0.5}
\newcommand*\spyviewer{axis cs:1.6,10}
\myplots
\node[very thin, rectangle, draw, minimum height=0.05\textwidth, minimum width=0.1\textwidth, inner sep=0pt] (spypoint) at (\spypoint) {};
\node[rectangle, draw, minimum height=0.25\textwidth, minimum width=0.5\textwidth, inner sep=0pt] (spyviewer) at (\spyviewer) {};
\draw (spypoint) edge (spyviewer);
\begin{scope}
    \clip (spyviewer) rectangle (0.25\textwidth-.5\pgflinewidth,0.5\textwidth-.5\pgflinewidth);
    \pgfmathparse{\spyfactor^2/(\spyfactor-1)}
    \begin{scope}[scale around={\spyfactor:($(\spyviewer)!\spyfactor^2/(\spyfactor^2-1)!(\spypoint)$)}]
        \myplots
    \end{scope}
\end{scope}
\end{axis}
\end{tikzpicture}%
\end{document}

1 Answer 1

5

You almost had it, you just need to define the \clip rectangle in terms of the spyviewer node:

\clip (spyviewer.south west) rectangle (spyviewer.north east);

\documentclass{memoir}
\usepackage{amsmath, tikz, pgfplots}
\usetikzlibrary{calc,spy,shapes}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
% [spy using outlines={rectangle,black,magnification=3,connect spies}]
\begin{axis}[%
width=6.01828521434821in,
height=4.74667979002625in,
unbounded coords=jump,
scale only axis,
xmin=1.25,
xmax=2,
xlabel={Robustness},
ymin=0,
ymax=20,
ylabel={Controller},
title={Case 11},
% legend style={draw=black,fill=white,legend cell align=left}
]
\newcommand*\myplots[1][]{
\addplot [
color=blue,
dashed
]
table[row sep=crcr]{
1.250000000054 0.218471486504557\\1.27500000012472 0.235880794037559\\1.30000000004985 0.253093675278193\\1.32499999988962 0.269996702493353\\1.34999999996296 0.28660169532495\\1.37500000003271 0.302803529131698\\1.4 0.318627316831094\\1.42500000000018 0.333967811176506\\1.44999999975398 0.348915976282584\\1.47499999999985 0.363393665978672\\1.50000000000007 0.377386190083728\\1.52499999999677 0.391065980885588\\1.54999999999987 0.404406696565785\\1.5749999999932 0.417357153126701\\1.60000000000001 0.429890180101467\\1.62500000007965 0.441495914781593\\1.64999999998047 0.453420186117445\\1.67500000000006 0.464885624062778\\1.70000000000013 0.475698022605379\\1.72499999999977 0.486001302461843\\1.75000000007258 0.49588760557761\\1.77500000001522 0.505406921339204\\1.79999999999992 0.514575948858101\\1.8249999999994 0.523580224222934\\1.84999999999966 0.532346980549898\\1.87499999998767 0.540884368439134\\1.90000000002483 0.549236128880721\\1.92499999997271 0.557390601810701\\1.94999999999657 0.565349329095157\\1.97499999999941 0.573100654346336\\1.99999999999993 0.580661122004988\\2.02499999998678 0.588035048696743\\2.0499999999709 0.595221272967935\\2.07499999999558 0.602215073143855\\2.09999999981776 0.609014392276178\\2.12500000001126 0.615634705868222\\2.14999999999957 0.622107376143054\\2.17499999994578 0.628535289209435\\2.19999999832383 0.63479954406909\\2.225 0.640975653060916\\2.24999999999955 0.647023272731091\\2.27499999999981 0.652900079435489\\2.29999999999833 0.658608487562972\\2.32499999999118 0.664162521737991\\2.34999999999773 0.669564611132752\\2.3750000000001 0.674786909878852\\2.40000000000024 0.679871708587361\\2.42499999998267 0.68481930558268\\2.44999999993369 0.689620895991369\\2.475 0.694300387671984\\2.50000000000226 0.698847019454327\\2.52499999999607 0.703231052229968\\2.54999999999282 0.707335949070966\\2.575 0.711299740037372\\2.59999999997469 0.715121564545835\\2.62500000002107 0.719030281788718\\2.64999999998823 0.722750679025289\\2.67499999999833 0.726355404984384\\2.6999999999989 0.729841563597055\\2.72500000000001 0.733213147420525\\2.75000000000046 0.736469697155365\\2.77500000011202 0.739681867953894\\2.8 0.742786117829059\\2.82500000000028 0.745831867778158\\2.84999999999055 0.748760245235199\\2.87499999999654 0.751587709302175\\2.90000000000011 0.754337713416536\\2.92500000001402 0.756957274828441\\2.94999999995396 0.759357524849041\\2.97499999999574 0.761687855897812\\3.00000000000154 0.764338301992918\\};
% \addlegendentry{po:pid};

\addplot [
color=green!50!black,
dashed
]
table[row sep=crcr]{
1.250000000054 0.590067068740046\\1.27500000012472 0.587559925194454\\1.30000000004985 0.586376714983635\\1.32499999988962 0.586069496190726\\1.34999999996296 0.586583764906821\\1.37500000003271 0.587586732327579\\1.4 0.589106686232638\\1.42500000000018 0.590799582313491\\1.44999999975398 0.592894933769734\\1.47499999999985 0.595115212158793\\1.50000000000007 0.597338496940835\\1.52499999999677 0.600163078036215\\1.54999999999987 0.603553183039573\\1.5749999999932 0.607355964366702\\1.60000000000001 0.611472392727962\\1.62500000007965 0.612640770551816\\1.64999999998047 0.618542970092304\\1.67500000000006 0.625478240488116\\1.70000000000013 0.63111283078678\\1.72499999999977 0.635924446858566\\1.75000000007258 0.640430875313834\\1.77500000001522 0.644633876853955\\1.79999999999992 0.647757322657836\\1.8249999999994 0.647552686193448\\1.84999999999966 0.647363620909126\\1.87499999998767 0.647117668558105\\1.90000000002483 0.646013166144435\\1.92499999997271 0.644422071010403\\1.94999999999657 0.642591297950079\\1.97499999999941 0.640866907916906\\1.99999999999993 0.639224831229371\\2.02499999998678 0.636685323281278\\2.0499999999709 0.633964341182746\\2.07499999999558 0.631446791852552\\2.09999999981776 0.628537387562625\\2.12500000001126 0.626107411465097\\2.14999999999957 0.624103000469\\2.17499999994578 0.62449136318196\\2.19999999832383 0.624449479840847\\2.225 0.6256877319412\\2.24999999999955 0.628088304645751\\2.27499999999981 0.630472877532383\\2.29999999999833 0.63283206246448\\2.32499999999118 0.635116015919806\\2.34999999999773 0.637322260692226\\2.3750000000001 0.640065236430338\\2.40000000000024 0.642534886174564\\2.42499999998267 0.644944092104574\\2.44999999993369 0.647313968075354\\2.475 0.649603381645786\\2.50000000000226 0.651905320157395\\2.52499999999607 0.654572686699921\\2.54999999999282 0.658065576936871\\2.575 0.661530686251933\\2.59999999997469 0.664985448106105\\2.62500000002107 0.667216476217143\\2.64999999998823 0.669895462979295\\2.67499999999833 0.672557424400875\\2.6999999999989 0.675234842685167\\2.72500000000001 0.677932291475736\\2.75000000000046 0.680659050710123\\2.77500000011202 0.683201448501821\\2.8 0.685708044714863\\2.82500000000028 0.688088365642656\\2.84999999999055 0.690571448381062\\2.87499999999654 0.693076214461211\\2.90000000000011 0.695575684929241\\2.92500000001402 0.698184293706798\\2.94999999995396 0.701235761040505\\2.97499999999574 0.704202908157281\\3.00000000000154 0.705980592056065\\};
}
\newcommand*\spyfactor{3.1622776601683793319988935444327}
\newcommand*\spypoint{axis cs:1.4,0.5}
\newcommand*\spyviewer{axis cs:1.6,10}
\myplots
\node[very thin, rectangle, draw, minimum height=0.05\textwidth, minimum width=0.1\textwidth, inner sep=0pt] (spypoint) at (\spypoint) {};
\node[rectangle, draw, minimum height=0.25\textwidth, minimum width=0.5\textwidth, inner sep=0pt] (spyviewer) at (\spyviewer) {};
\draw (spypoint) edge (spyviewer);
\begin{scope}
    \clip (spyviewer.south west) rectangle (spyviewer.north east);
    \pgfmathparse{\spyfactor^2/(\spyfactor-1)}
    \begin{scope}[scale around={\spyfactor:($(\spyviewer)!\spyfactor^2/(\spyfactor^2-1)!(\spypoint)$)}]
        \myplots
    \end{scope}
\end{scope}
\end{axis}
\end{tikzpicture}%
\end{document}
4
  • Ah, awesomeness. Couldn't figure that one out...
    – Holene
    May 29, 2013 at 14:22
  • This code doesn't have the same output as in your picture. The spy box is completely blank.
    – joaocandre
    Jul 17, 2013 at 14:19
  • @joaocandre: Works for me (TikZ 2.1, pdflatex). What version of TikZ are you using, and which compiler?
    – Jake
    Jul 17, 2013 at 14:27
  • Also TikZ 2.1, doesn't work with either latexmk and pdflatex. What exactly would I have to change in your code to just amplify 1000x the last, say, 20 values of the plot? I don't really mind having the spyfactor hardcoded into the file.
    – joaocandre
    Jul 17, 2013 at 14:45

You must log in to answer this question.

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