26

I am using pgfplots to plot some data I have, and I wanted to generate a large plot with another plot inside of it, showing a zoom on a region of the large plot. I have noticed that when you do

\begin{tikzpicture}
 \begin{axis}[width=0.45\textwidth]
  %small plot
 \end{axis}
 \begin{axis}[width=\textwidth]
  %large plot
 \end{axis}
\end{tikzpicture}

the second plot starts at the same position than the first one. Is there a way to move the origin of the small plot to an empty region of the big one? Or maybe you know a better alternative...

thanks

0

3 Answers 3

16

You could use the xshift, and yshift keys:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
 \begin{axis}[xshift=.5\textwidth,yshift=2cm,width=0.45\textwidth]
  %small plot
 \end{axis}
 \begin{axis}[width=\textwidth]
  %large plot
 \end{axis}
\end{tikzpicture}

\end{document}
3
  • One more question to this (I hope someone will still read this after one year): If I have not decided on the final size of the plot yet, it would be convenient to position and size the small plot relative to the large one. Is that somehow possible?
    – Janek
    Oct 4, 2012 at 15:00
  • 3
    @Janek sure it's possible. You can give a name to the main plot and then use this name to place the auxiliary one; for example, \documentclass{article} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis}[width=\textwidth,name=mainplot] \addplot {x}; \end{axis} \begin{axis}[at={(mainplot.south east)},anchor=south east,yshift=1cm,width=0.45\textwidth] \addplot {x}; \end{axis} \end{tikzpicture} \end{document}. Oct 5, 2012 at 2:05
  • @GonzaloMedina Can you please add you comment as an answer; I accidentally nearly repeated this as an answer. As soon as your answer is in place, I will delete mine.
    – DennisH
    Dec 8, 2016 at 11:11
30

Depending on what exactly you're trying to highlight with the inset, the spy library might be useful for you. It creates an insert with a magnified part of a TikZ picture.

pgfplots and spy

In order to use the axis cs: coordinate system for specifying which part to magnify, you have to create a coordinate node in the axis, as axis cs: is not available inside the \spy command:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{spy}
\begin{document}

\begin{tikzpicture}[spy using outlines={rectangle, magnification=3,connect spies}]
\begin{axis}[grid=major,no markers,domain=-5:5,enlargelimits=false]
\addplot {x^2};
\addplot {x^3};

\coordinate (spypoint) at (axis cs:0,0);
\coordinate (spyviewer) at (axis cs:0.5,-90);
\spy[width=6cm,height=1cm] on (spypoint) in node [fill=white] at (spyviewer);
\end{axis}

\end{tikzpicture}

\end{document}
2
  • that's a nice alternative, not quite what I was looking for but still...
    – yannick
    May 23, 2011 at 17:26
  • Hi @Jake, Is there a way to manage ytick inside the spyviewer? I mean, I would like to get not only a magnification, but also set the ytick by which I could get a clearer visualization, given that I'm dealing with a groupplot and data tables.
    – Catarella
    Jun 25, 2021 at 16:53
9

You can place an inset relatively to the main plot by doing

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[remember picture]
    \begin{axis}[width=\textwidth]
    %large plot
    \addplot {x^3};
    \coordinate (insetPosition) at (rel axis cs:0.95,0.05);
    \end{axis}
    \begin{axis}[at={(insetPosition)},anchor={outer south east},footnotesize]
    %small plot
    \addplot {2*x};
    \end{axis}
\end{tikzpicture}
\end{document}

which then gives you

Automatically shifted inset

This automatically shifts to make space for the subsets axis descriptions too. Occasionally one might want to place such a subset at a fixed coordinate, in which case you only need to exchange the definition of insetPosition to

\coordinate (insetPosition) at (axis cs:5:-140);

to achieve this effect.

2
  • Am I right that this is almost the same solution as Gonzalo Medina has provided in this comment below the question (already)? Dec 8, 2016 at 10:13
  • @StefanPinnow Thank you; I missed that. I asked Gonzalo Medina to add his comment as an answer (so that not more people miss this option of adding an inset in this way) and will then remove this answer.
    – DennisH
    Dec 8, 2016 at 11:13

You must log in to answer this question.

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