# Draw an inset for two unrelated plots

Suppose I have the following voltage vs. time data set, recorded by a decent oscillosocpe:

As you can see, there is some very fast changes between t=0 and t=1 sec for this measurement.

So I recorded this physical phenomena again, but now on a shorter time window, so the noisy part can be seen better:

Good! I have two csv files that I can plot with pgfplots.

What I am asking is how to produce this figure:

Using tikz's spy library, you can zoom in on a portion of a TikZ picture.

This drawing above is very similar in its appearance to tikz's spy tool, but it is made of two "completely unrelated" plots. spy is good only for making an inset of one plot.

Any one have an idea?

Note: These plots were created with Desmos and Windows paint. If you want to post an answer then a circle spied by a triangle is good too. This wiggling unrealistic waveform is just for demonstration purpose, to clarify the idea.

Thanks!

Update:

I read now the tikz manual and came across the Coordinate transformations subsection (25.3).

I just ran the following code, to compile successfully:

\documentclass[border=2pt]{standalone}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=major,
]
\end{axis}
\begin{scope}[xshift=8em,yshift=-20em]
\begin{axis}[
grid=major,
]
\end{axis}
\end{scope}
\draw[red,thick] (0,0) -- (2,1); % <--- A decoration
\end{tikzpicture}
\end{document}


So I got two different axis environments, separated one from the other by the {scope}[xshift , yshift]. Now I added a thick red line from (0,0) to (2,1) but those two coordinates are not sticking to the (first) plot coordinates system, as you can see from the axis ticks.

I changed that to

\draw[red,thick] (axis cs:0,0) -- (2,1);


and got an error

Missing \endcsname inserted. \pgfcrdmth@x l.18 \draw[red,thick] (axis cs:0,0) -- (2,1); ?

So I need to give this coordinate system a name, so TeX know whom I refer to? How do I do that?

• Instead of using the spy library, why not just plot the two plots, draw the rectangles, and then the diagonal lines connecting them? – Willie Wong May 4 at 18:35
• @WillieWong Yeah I guess it is a solution. How can I align the two pictures, one slightly to the right and slightly to the bottom? And then, what are the coordinate specifications for drawing the lines and the two rectangles? – tush May 4 at 18:38
• Accordingly, I changed the question's title. – tush May 4 at 18:49
• I'm somewhat confident that this won't be possible using the spy tool as that works by enlarging parts of the already existing image. Thus as already has been noted, you should just draw the decorations yourself. – Raven May 4 at 18:49

Are you looking for one of those?

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{axis}[name=axis A,
grid=major,
]
\end{axis}
\begin{scope}[xshift=8em,yshift=-20em]
\begin{axis}[name=axis B,
grid=major,
]
\end{axis}
\end{scope}
\draw[red,thick] (axis A.north east) -- (axis B.north east)
(axis A.south west) -- (axis B.south west);
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[grid=major,
]
\draw (0,-2) coordinate (bl) rectangle (1,2) coordinate (tr);
\end{axis}
\begin{scope}[xshift=8em,yshift=-20em]
\begin{axis}[name=axis B,
grid=major,
]
\end{axis}
\end{scope}
\draw[red,thick] (tr) -- (axis B.north east)
(bl) -- (axis B.south west);
\end{tikzpicture}
\end{document}


• Well that looks quite promising. Tell me, where the nodes bl and tr are first defined? – tush May 6 at 20:45
• @tush \draw (0,-2) coordinate (bl) rectangle (1,2) coordinate (tr); – user241266 May 6 at 20:45
• So bl is defined to be at (0,-2) and the same for tr to be at (1,2)? – tush May 6 at 20:46
• @tush precisely. The other corners are "known" because I gave the axis objects names, so they are just rectangular nodes the anchors of which one can use. – user241266 May 6 at 20:47
• If you are already on the track, can you show me how to start the inset of the upper plot (+x^2 parabola) to start from somewhere in the middle of its canvas, as demonstrated in the original post? – tush May 6 at 20:49

In this case, I imported my underlying graphs as images, so you could focus on the syntax of the \stackinset, where I place argument #5 (the "spy" boxes) atop argument #6 (the underlying graphics), in this case, aligned at the bottom-right corner. However, you could replace each of the \includegraphics calls with (in this case 3cm x 3cm) tikz drawings, or you could create them in standalone mode and import them, as I do here.

The key is to lay out the two images in a size you like, and that will provide you the graph coordinates to use for your "spy" boxes.

\documentclass{article}
\usepackage{tikz,stackengine}
\begin{document}
\stackinset{r}{}{b}{}{%
\begin{tikzpicture}
\draw [thick] (1,0) -- (4,0) -- (4,3) -- (1,3) -- (1,0);
\draw [thick] (.25,4) -- (.75,4) -- (.75,6) -- (.25,6) -- (.25,4);
\draw [thick] (.25,4) -- (1,0);
\draw [thick] (.75,6) -- (4,3);
\end{tikzpicture}%
}{%
\stackon[.5cm]
{\kern1cm\includegraphics[width=3cm,height=3cm]{example-image-a}}%
{\includegraphics[width=3cm,height=3cm]{example-image-b}\kern1cm}%
}
\end{document}


Using the OP's sample graph, I could adapt. However, I highly recommend setting your graph up in a way where you know exact size of your images. Further, while both graphs are done in a single tikz picture, it may prove simpler to stack them as two separate tikz constructions.

\documentclass{article}
\usepackage{stackengine}
\usepackage{tikz,pgfplots}
\begin{document}
\stackinset{r}{.2cm}{b}{.5cm}{%
\begin{tikzpicture}
\draw [thick] (3.3,0) -- (10,0) -- (10,5.5) -- (3.3,5.5) -- (3.3,0);
\draw [thick] (.5,7.0) -- (2,7.0) -- (2,8.5) -- (.5,8.5) -- (.5,7.0);
\draw [thick] (.5,7.0) -- (3.3,0);
\draw [thick] (2,8.5) -- (10,5.5);
\end{tikzpicture}%
}{%
\begin{tikzpicture}
\begin{axis}[
grid=major,
]
\end{axis}
\begin{scope}[xshift=8em,yshift=-20em]
\begin{axis}[
grid=major,
]

• Thanks a lot! Since this drawing is going to be included in a rather long project, ~35 pages in length, with some other 25 or so drawing, I rather prefer to keep my self the ability to save, and include, this graphics as a tikz file. Is it possible with your solution? Moreover, these waveforms to be plotted are generated from csv lists, with the horizontal axis meaning time, so I want the decorations to be drawn through the axis coordinates. This is the reason for my tendency to use (axis cs:_,_) in the original post. I think that I will write down all that information. – tush May 6 at 20:19
• @tush I am not a tikz expert and so I can't really answer your questions off the cuff (I am not sure what you mean by "tikz file"). I see you provided sample code to create two sample graph I will work to show how it is done with my method. – Steven B. Segletes May 6 at 20:22