4

I want to create a plot with a second, smaller plot as an inset. This is easy enough using the following code, where the widths of the plots are defined. However, I often use figures in several different documents like two-column papers, presentations and my thesis, each of which have different \textwidths. I therefore use tikzscale to scale the figure to the appropriate size while preserving font-sizes etc. This, however, implies, that I should not define a width for the axis environment as shown in this question. But if I don't define a fixed width for the main plot, then I also cannot define a width for the inset.
Is there a possibility to set the widthof the inset to a value relative to the main plot's width without having given the main plot an explicit width (i.e. the main plot's widthis unknown)?

\documentclass[crop,10pt]{standalone}
\usepackage[english]{babel}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width=12cm,
    name=mainplot,
    enlargelimits=false,
    ]
    \addplot coordinates {(0,0) (1,1) (2,2)};
\end{axis}
\begin{axis}[
    width=4cm,
    at={($(mainplot.north west) + (rel axis cs:0.1,-0.1)$)},
    anchor=north west,
    enlargelimits=false,
    ]
    \addplot coordinates {(0,2) (1,1) (2,0)};
\end{axis}
\end{tikzpicture}
\end{document}

1 Answer 1

3

Here is a not entirely perfect way that sets the width of the inner axis relative to the outer axis. The usage of \pgfgetlastxy is taken from Peter Grill's answer to Extract x, y coordinate of an arbitrary point in TikZ. veclen(x,y) calculates the length of the vector (x,y).

Notes:

  • When using mainplot.north west and mainplot.north east, the length you get is the width of the axis without legend, ticklabels and labels. If you want the new width relative to to the total width of the outer plot, use outer north west and outer north east.

  • The width that is calculated is actually a little bit less than the width of mainplot. I don't know why, probably something really obvious I've missed.

  • Though I notice that when using north west/north east (not the outer ones), and adding scale only axis to the inner axis, the relative scale is correct.

enter image description here

\documentclass[border=3mm]{standalone}
\usepackage[english]{babel}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    name=mainplot,
    enlargelimits=false,
    ]
    \addplot coordinates {(0,0) (1,1) (2,2)};
\end{axis}
\path (mainplot.outer north west); \pgfgetlastxy{\xw}{\yw}
\path (mainplot.outer north east); \pgfgetlastxy{\xe}{\ye}
\pgfmathsetmacro\miniplotwidth{veclen(\xe-\xw,0)*0.5}
\begin{axis}[
    width=\miniplotwidth,
    at={($(mainplot.north west) + (rel axis cs:0.05,-0.05)$)},
    anchor=outer north west,
    enlargelimits=false,
    ]
    \addplot coordinates {(0,2) (1,1) (2,0)};
\end{axis}
\end{tikzpicture}
\end{document}
6
  • If I didn't understand the problem correctly, please let me know, I'll just delete the answer. Jun 20, 2014 at 19:33
  • Unfortunately, this does not answer my question. I'll try to make this more clear in my question. The thing is, that I do not want to give the main plot an explicit width as it is to be scaled by tikzscale. Yet, I still want the inset to have a certain relative width with respect to the main plot.
    – Janek
    Jun 23, 2014 at 14:29
  • @Janek, Torbjørn's answer is working perfectly also when you comment the line starting with width. Because in the question your MWE doesn't contain some tikzscale code nor it that package mentioned (in the question) at all we cannot reproduce your problem, if there would be any regarding Torbjørn's code together with tikzscale, please edit your question accordingly or the question will be closed as "unclear what you are asking" to get it of the "still open question" list. Many thanks. Feb 4, 2017 at 8:14
  • @StefanPinnow Janek's comment was made before I edited my answer, the original version used a different method (see revision history). Can't remember why I didn't ping him when I edited, but even at that time it was half a year since he was logged on. Feb 4, 2017 at 8:24
  • @StefanPinnow , the MWE does not contain tikzscale because the problem is not related to it. The question is about creating an inset plot with a width relative to an (unknown) variable main plot width. I only mentioned tikzscale to explain why I do not just define a fixed main plot width. You are right, however, that by deleting the line starting with width the code is working as intended.
    – Janek
    Jun 9, 2017 at 13:49

You must log in to answer this question.

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