1

Is it possible to have a smaller plot embedded into one of the plots of a groupplot? What I would like to achieve is like a magnified region of the lower plot, it should display two rectangles, one smaller that indicates the region magnified, and another a little bigger where the magnification is visible. I've tried to use \usetikzlibrary{spy}, but it does just a simple zoom of a region. I would like to have the control of the ytick parameter of the magnified region, in order to "stretch" potential curves that appears overlapped. The graphical structure I would like to get is the following:

enter image description here

And here a dummy MWE to try out a possible solution:

\documentclass{article}
    \usepackage[dvipsnames]{xcolor}
    \usepackage{pgf,pgfplots,pgfplotstable}
    \pgfplotsset{compat=newest}
    \usepgfplotslibrary{colormaps}
    \usepgfplotslibrary{groupplots}
    %\usetikzlibrary{spy}

\begin{filecontents}{data1.dat} t1 a1 t2 a2 t3 a3 0.142 1.000 0.154 1.000 0.139 0.995 0.284 1.000 0.308 1.000 0.279 0.990 0.426 1.000 0.461 1.000 0.418 0.989 0.569 1.000 0.615 1.000 0.558 0.989 0.711 1.000 0.769 1.000 0.697 0.989 0.853 1.000 0.923 1.000 0.836 0.989 0.995 1.000 1.077 1.000 0.976 0.989 1.137 1.000 1.230 1.000 1.115 0.989 1.279 1.000 1.384 1.000 1.255 0.989 1.422 0.995 1.538 1.000 1.394 0.986 1.564 0.990 1.692 1.000 1.534 0.981
\end{filecontents
}

\begin{filecontents}{data2.dat} t1 a1 t4 a4 t3 a3 3.554 0.920 3.845 0.950 3.485 0.913 3.696 0.915 3.999 0.945 3.625 0.908 3.838 0.910 4.153 0.939 3.764 0.903 3.980 0.905 4.306 0.934 3.904 0.898 4.123 0.900 4.460 0.929 4.043 0.893 4.265 0.895 4.614 0.923 4.182 0.888 4.407 0.890 4.768 0.918 4.322 0.883 4.549 0.885 4.922 0.912 4.461 0.878 4.691 0.880 5.075 0.907 4.601 0.873 4.833 0.875 5.229 0.902 4.740 0.868 4.976 0.870 5.383 0.896 4.880 0.864 5.118 0.865 5.537 0.891 5.019 0.859 5.260 0.860 5.691 0.885 5.158 0.854
\end{filecontents
}

\begin{document}

\begin{figure}[htbp] \centering \begin{tikzpicture}[scale=0.9]%,spy using outlines={rectangle, magnification=1.5}] \begin{groupplot}[group style={ group name=rzrxcomp, group size=1 by 2, xlabels at=edge bottom, ylabels at=edge left, vertical sep=4em }, xmin = 0, xmax = 2, ymin = 0.8, ymax = 1.1, xtick distance = 0.2, ytick distance = 0.2, xlabel={$time;[s]$}, ylabel={$mass$}, grid = both, grid style = {dotted}, minor tick num = 1, major grid style = {lightgray!75}, minor grid style = {lightgray!75}, width = 0.85\textwidth, height = 0.50\textwidth, legend style={at={(0.5,-0.3)}, anchor=north,legend columns=-1}, legend cell align = {left}, ]

\nextgroupplot

\addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data1.dat}; \addplot[smooth, Lavender, very thick] table [x = {t2}, y = {a2}] {data1.dat}; \addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data1.dat};

\nextgroupplot[xmin = 3, xmax = 6, ymin = 0.7, ymax = 1, ytick distance = 0.03, legend entries={n1,n2,n3,n4}]

\addlegendimage{black, very thick, no markers} \addlegendimage{Lavender, very thick, no markers} \addlegendimage{Violet, very thick, no markers} \addlegendimage{BurntOrange, dotted, very thick, no markers}

\addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data2.dat}; \addplot[smooth, BurntOrange, dotted, very thick] table [x = {t4}, y = {a4}] {data2.dat}; \addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data2.dat};

%\coordinate (spypoint) at (axis cs:xx,yy); %\coordinate (spyviewer) at (axis cs:xx,yy); %\spy[width=3cm,height=2cm] on (spypoint) in node [fill=white] at (spyviewer);

\end{groupplot} \end{tikzpicture} \end{figure}

\end{document}

I don't know if with spy it's possible to control the ytick of the spyviewer, that's why I have thought about nested groupplots. Anyway, If it could help, inside the MWE you can find commented % the spy related parts.

Catarella
  • 361

1 Answers1

1

You can add a new axis environment after \end{groupplot} and plot the same thing in that one. Here I used the calc library to position this new axis. (It's not loaded explicitly, with \usetikzlibrary{calc}, but it's loaded by one of the other libraries, I suppose.)

For this axis environment set the axis limits, tick options etc. as you see fit.

Of course, this way everything is plotted twice. Not a big deal with these datasets, but might be noticeable with large datasets.

\documentclass{article}
    \usepackage[dvipsnames]{xcolor}
    \usepackage{pgfplotstable} % loads pgfplots which loads pgf
    \pgfplotsset{compat=newest}
    \usepgfplotslibrary{colormaps}
    \usepgfplotslibrary{groupplots}

\begin{filecontents}{data1.dat} t1 a1 t2 a2 t3 a3 0.142 1.000 0.154 1.000 0.139 0.995 0.284 1.000 0.308 1.000 0.279 0.990 0.426 1.000 0.461 1.000 0.418 0.989 0.569 1.000 0.615 1.000 0.558 0.989 0.711 1.000 0.769 1.000 0.697 0.989 0.853 1.000 0.923 1.000 0.836 0.989 0.995 1.000 1.077 1.000 0.976 0.989 1.137 1.000 1.230 1.000 1.115 0.989 1.279 1.000 1.384 1.000 1.255 0.989 1.422 0.995 1.538 1.000 1.394 0.986 1.564 0.990 1.692 1.000 1.534 0.981
\end{filecontents
}

\begin{filecontents}{data2.dat} t1 a1 t4 a4 t3 a3 3.554 0.920 3.845 0.950 3.485 0.913 3.696 0.915 3.999 0.945 3.625 0.908 3.838 0.910 4.153 0.939 3.764 0.903 3.980 0.905 4.306 0.934 3.904 0.898 4.123 0.900 4.460 0.929 4.043 0.893 4.265 0.895 4.614 0.923 4.182 0.888 4.407 0.890 4.768 0.918 4.322 0.883 4.549 0.885 4.922 0.912 4.461 0.878 4.691 0.880 5.075 0.907 4.601 0.873 4.833 0.875 5.229 0.902 4.740 0.868 4.976 0.870 5.383 0.896 4.880 0.864 5.118 0.865 5.537 0.891 5.019 0.859 5.260 0.860 5.691 0.885 5.158 0.854
\end{filecontents
}

\begin{document}

\begin{figure}[htbp] \centering \begin{tikzpicture}[scale=0.9]%,spy using outlines={rectangle, magnification=1.5}] \begin{groupplot}[group style={ group name=rzrxcomp, group size=1 by 2, xlabels at=edge bottom, ylabels at=edge left, vertical sep=4em }, xmin = 0, xmax = 2, ymin = 0.8, ymax = 1.1, xtick distance = 0.2, ytick distance = 0.2, xlabel={$time;[s]$}, ylabel={$mass$}, grid = both, grid style = {dotted}, minor tick num = 1, major grid style = {lightgray!75}, minor grid style = {lightgray!75}, width = 0.85\textwidth, height = 0.50\textwidth, legend style={at={(0.5,-0.3)}, anchor=north,legend columns=-1}, legend cell align = {left}, ]

\nextgroupplot

\addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data1.dat}; \addplot[smooth, Lavender, very thick] table [x = {t2}, y = {a2}] {data1.dat}; \addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data1.dat};

\nextgroupplot[xmin = 3, xmax = 6, ymin = 0.7, ymax = 1, ytick distance = 0.03, legend entries={n1,n2,n3,n4}, ]

\addlegendimage{black, very thick, no markers} \addlegendimage{Lavender, very thick, no markers} \addlegendimage{Violet, very thick, no markers} \addlegendimage{BurntOrange, dotted, very thick, no markers}

\addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data2.dat}; \addplot[smooth, BurntOrange, dotted, very thick] table [x = {t4}, y = {a4}] {data2.dat}; \addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data2.dat};

\end{groupplot}

\begin{axis}[ footnotesize, % simple way of reducing fontsizes etc. a bit xmin = 3.5, xmax = 4.5, ymin = 0.9, ymax = 1, ytick distance = 0.02, at={($(rzrxcomp c1r2.north east)+(-2mm,-2mm)$)}, % position anchor=north east, % anchor scale only axis, width=2cm ] \addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data2.dat}; \addplot[smooth, BurntOrange, dotted, very thick] table [x = {t4}, y = {a4}] {data2.dat}; \addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data2.dat};

\end{axis} \end{tikzpicture} \end{figure}

\end{document}

Torbjørn T.
  • 206,688
  • Thank you very much @TorbjørnT, the code worked amazingly. Just one last request... How could I draw a rectangle and place it in the north-west corner like the image in my main post? It should indicate the "magnified" region. – Catarella Jun 26 '21 at 18:55
  • 1
    @Catarella \draw (3.5,0.9) rectangle (4.5,1); for example, where the coordinates correspond to the axis limits of the inset axis. – Torbjørn T. Jun 26 '21 at 19:00