3

I am trying to insert an inset into the main figure using pgfplots. Here is what I've got so far.enter image description here

I want the outcome to be something like the figure shown in the picture below. What would be the easiest way of doing that.enter image description here

Here is the code I have wrote so far

    \documentclass[a4paper]{article}

\usepackage{amsmath}
\usepackage{pgfplots}
\usepackage{pgfplotstable}%fitting functions
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\pgfkeys{/pgf/number format/.cd,1000 sep={}}


\newcommand{\myfont}{\fontfamily{cmss}\selectfont}% used with mathpazo 


\pgfplotsset{compat=1.15}
\pgfplotsset{label style={font=\Large},
            tick label style={font=\Large}}

\pgfplotsset{error bars/.cd,
    x dir=both, x explicit,
    y dir=both, y explicit,
    }
\pgfplotsset{/pgfplots/error bars/error bar style={semithick,black}}
%\pgfplotsset{/pgfplots/label shift={0pt}}


\begin{document}


\begin{figure}
\centering

\begin{tikzpicture}
\begin{axis}[
    name=plot1,
    enable tick line clipping=false, % makes all ticks same width
    axis line style=semithick,
    width=9cm,
    height=7cm,
    legend style={at={(0.95,0.35)},draw=none},
    legend cell align={right},
    x tick style={black,semithick},
    x label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    xlabel={{$\lambda^2-\dfrac{1}{\lambda}$} [{\myfont -}]},
    xmin=0,xmax=70,
    xtick={0,10,...,70},
    xtick pos=bottom,
    %minor x tick num=1,
    xtick align=outside,
    %xminorgrids=true,
    y tick style={black,semithick},
    y label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    ylabel={{$\sigma_t$} [{\myfont MPa}]},
    ymin=0,ymax=30,
    ytick={0,10,...,30},
    ytick pos=left,
    %minor y tick num=1,
    ytick align=outside,
    %yminorgrids=true,
]
    \addplot [black,very thick] file {tensilePPmain.txt};

    \node at (axis cs: 56,12) {\Large{\myfont $0.4$\,MPa}};

    \draw [semithick] (30,8.9) -- (45,8.9);

    \draw [semithick] (45,8.9) -- (45,13.8);

\end{axis}
\end{tikzpicture}


\begin{tikzpicture}
\begin{axis}[
    enable tick line clipping=false, % makes all ticks same width
    axis line style=semithick,
    width=5.5cm,
    height=4.5cm,
    axis background style={fill=white},
    legend style={at={(0.95,0.35)},draw=none},
    legend cell align={right},
    x tick style={black,semithick},
    x label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    xlabel={{$\epsilon_n$} [{\myfont -}]},
    xmin=0,xmax=2,
    xtick={0,1,...,2},
    xtick pos=bottom,
    xtick align=outside,
    y tick style={black,semithick},
    y label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    ylabel={{$\sigma_n$} [{\myfont MPa}]},
    ymin=0,ymax=2,
    ytick={0,1,...,2},
    ytick pos=left,
    ytick align=outside,
    %at=(plot1.axis cs:0,0),    
]
    \addplot [black,very thick] file {tensilePPinset.txt};

    \addplot[mark=x,mark options={scale=2,very thick}] coordinates {(0.52,0.74)};

    \node at (axis cs: 1,1.5) {\Large{\myfont $\sigma_{y0}=0.8$\,MPa}};

    \draw [semithick,dashed] (0,0.74) -- (0.52,0.74);

    \draw [semithick,dashed] (0.52,0) -- (0.52,0.74);

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


\end{document}

4 Answers 4

2

You were absolutely on the right track. You (just) need to place both axis environments in the same tikzpicture environment.

(Please note that I heavily simplified your code so it more obvious what has to be done to achieve what you want.)

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        name=plot1,
    ]
        \addplot {-x};
    \end{axis}
    \begin{axis}[
        at={(plot1.north east)},
        xshift=-5pt,
        yshift=-5pt,
        anchor=north east,
        tiny,
    ]
        \addplot {x};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

2
  • How can I specify the location of the inset precisely rather than using "north east"?
    – Gagik
    Feb 6, 2019 at 1:10
  • You can place the inset wherever you want using the at key absolute or relative to whatever is known up to there. If a relative positioning isn't precise enough you can further shift the inset using the xshift and yshift keys as I did. Sorry, your question is too brood. Feb 6, 2019 at 5:10
2

Place both axis environments in the same tikzpicture environment. You can't however use plot1.axis cs:.. like you tried, plot1 becomes a like a node with specific anchors. So you can for example use at=(plot1.north west) to place the second axis' anchor in the top left corner of the first axis.

However, it might be more convenient to define a helper coordinate in the first axis, with e.g. \coordinate (otheraxispos) at (20,30);, and then use anchor=north west,at=(otheraxispos) in the options of the second axis.

Because I don't have your datafile, I used some random numbers instead. I also reduced the font size and size of the inset axis.

To get a white fill behind the inset you could add \fill[white] (otheraxispos) rectangle +(4.5cm,-3.5cm); before the second axis environment, and set anchor=outer north west for the second axis. It's not particularly elegant, but it does work at least. Here 4.5cm and 3.5cm correspond to the width and height defined for the inset axis.

(As described in the manual section 4.10.1 Common scaling options those dimensions are just approximations, so you might want to/have to change them a little bit.)

enter image description here

\documentclass[a4paper]{article}

\usepackage{amsmath}

\usepackage{pgfplotstable}%fitting functions, loads pgfplots which loads tikz
\usetikzlibrary{tikzmark}

\pgfkeys{/pgf/number format/.cd,1000 sep={}}


\newcommand{\myfont}{\fontfamily{cmss}\selectfont}% used with mathpazo 


\pgfplotsset{
   compat=1.15,
   label style={font=\Large},
   tick label style={font=\Large},
   error bars/.cd,
     x dir=both, x explicit,
     y dir=both, y explicit,
     error bar style={semithick,black}
}
%\pgfplotsset{/pgfplots/label shift={0pt}}


\begin{document}


\begin{figure}
\centering

\begin{tikzpicture}
\begin{axis}[
    name=plot1,
    enable tick line clipping=false, % makes all ticks same width
    axis line style=semithick,
    width=9cm,
    height=7cm,
    legend style={at={(0.95,0.35)},draw=none},
    legend cell align={right},
    x tick style={black,semithick},
    x label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    xlabel={{$\lambda^2-\dfrac{1}{\lambda}$} [{\myfont -}]},
    xmin=0,xmax=70,
    xtick={0,10,...,70},
    xtick pos=bottom,
    %minor x tick num=1,
    xtick align=outside,
    %xminorgrids=true,
    y tick style={black,semithick},
    y label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    ylabel={{$\sigma_t$} [{\myfont MPa}]},
    ymin=0,ymax=30,
    ytick={0,10,...,30},
    ytick pos=left,
    %minor y tick num=1,
    ytick align=outside,
    %yminorgrids=true,
]
    \pgfmathsetseed{42}
    \addplot [black,very thick,domain=0:70] {rnd+x*0.3};

    \node at (56,12) {\Large{\myfont $0.4$\,MPa}};

    \draw [semithick] (30,8.9) -- (45,8.9);

    \draw [semithick] (45,8.9) -- (45,13.8);


    \coordinate (otheraxispos) at (5,32);

\end{axis}

\fill[white] (otheraxispos) rectangle +(4.5cm,-3.5cm);

\begin{axis}[
    enable tick line clipping=false, % makes all ticks same width
    axis line style=semithick,
    footnotesize,
    width=4.5cm,
    height=3.5cm,
    legend style={at={(0.95,0.35)},draw=none},
    legend cell align={right},
    x tick style={black,semithick},
    x label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    xlabel={{$\epsilon_n$} [{\myfont -}]},
    xmin=0,xmax=2,
    xtick={0,1,...,2},
    xtick pos=bottom,
    xtick align=outside,
    y tick style={black,semithick},
    y label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    ylabel={{$\sigma_n$} [{\myfont MPa}]},
    ymin=0,ymax=2,
    ytick={0,1,...,2},
    ytick pos=left,
    ytick align=outside,
    anchor=outer north west,
%    at=(plot1.north west),
    at=(otheraxispos)
]
    \pgfmathsetseed{42}
    \addplot [black,very thick,domain=0:70] {rnd+x*0.3};

    \addplot[mark=x,mark options={scale=2,very thick}] coordinates {(0.52,0.74)};

    \node at (axis cs: 1,1.5) {\myfont $\sigma_{y0}=0.8$\,MPa};

    \draw [semithick,dashed] (0,0.74) -- (0.52,0.74);

    \draw [semithick,dashed] (0.52,0) -- (0.52,0.74);

\end{axis}


\end{tikzpicture}
\end{figure}


\end{document}
3
  • The trouble with doing it the way you suggested is that the inset overlaps with the main graph. Look for instance the tick #2 of the y axis. I want the inset to be at the front, on top of the main graph.
    – Gagik
    Feb 5, 2019 at 20:24
  • @Gagik Sorry, missed that. You could add \fill[white] (otheraxispos) rectangle +(4.5cm,-3.5cm); before the inset axis, where the lengths correspond to the width/height of the inset. Also you need to change the anchor to outer north west. Will edit. Feb 5, 2019 at 21:07
  • @Gagik Note though that the inset is at the front, on top of, the main graph. But the nodes for the ticklabels etc. doesn't have any background fill. Feb 5, 2019 at 21:18
2

Here, I used the \stackinset macro from stackengine to do it. I also scaled the inset down to 70% by way of a \scalebox.

\documentclass[a4paper]{article}

\usepackage{amsmath}
\usepackage{pgfplots}
\usepackage{pgfplotstable}%fitting functions
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\pgfkeys{/pgf/number format/.cd,1000 sep={}}


\newcommand{\myfont}{\fontfamily{cmss}\selectfont}% used with mathpazo 


\pgfplotsset{compat=1.15}
\pgfplotsset{label style={font=\Large},
            tick label style={font=\Large}}

\pgfplotsset{error bars/.cd,
    x dir=both, x explicit,
    y dir=both, y explicit,
    }
\pgfplotsset{/pgfplots/error bars/error bar style={semithick,black}}
%\pgfplotsset{/pgfplots/label shift={0pt}}

\usepackage{stackengine}
\begin{document}


\begin{figure}
\centering
\stackinset{c}{}{t}{-5pt}{%
\scalebox{.7}{\begin{tikzpicture}
\begin{axis}[
    enable tick line clipping=false, % makes all ticks same width
    axis line style=semithick,
    width=5.5cm,
    height=4.5cm,
    axis background style={fill=white},
    legend style={at={(0.95,0.35)},draw=none},
    legend cell align={right},
    x tick style={black,semithick},
    x label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    xlabel={{$\epsilon_n$} [{\myfont -}]},
    xmin=0,xmax=2,
    xtick={0,1,...,2},
    xtick pos=bottom,
    xtick align=outside,
    y tick style={black,semithick},
    y label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    ylabel={{$\sigma_n$} [{\myfont MPa}]},
    ymin=0,ymax=2,
    ytick={0,1,...,2},
    ytick pos=left,
    ytick align=outside,
    %at=(plot1.axis cs:0,0),    
]
    \addplot [black,very thick]  {rnd+x*0.3};
%
    \addplot[mark=x,mark options={scale=2,very thick}] coordinates {(0.52,0.74)};
%
    \node at (axis cs: 1,1.5) {\Large{\myfont $\sigma_{y0}=0.8$\,MPa}};
%
    \draw [semithick,dashed] (0,0.74) -- (0.52,0.74);
%
    \draw [semithick,dashed] (0.52,0) -- (0.52,0.74);
%
\end{axis}
\end{tikzpicture}%
}}{%
\begin{tikzpicture}
\begin{axis}[
    name=plot1,
    enable tick line clipping=false, % makes all ticks same width
    axis line style=semithick,
    width=9cm,
    height=7cm,
    legend style={at={(0.95,0.35)},draw=none},
    legend cell align={right},
    x tick style={black,semithick},
    x label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    xlabel={{$\lambda^2-\dfrac{1}{\lambda}$} [{\myfont -}]},
    xmin=0,xmax=70,
    xtick={0,10,...,70},
    xtick pos=bottom,
    %minor x tick num=1,
    xtick align=outside,
    %xminorgrids=true,
    y tick style={black,semithick},
    y label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    ylabel={{$\sigma_t$} [{\myfont MPa}]},
    ymin=0,ymax=30,
    ytick={0,10,...,30},
    ytick pos=left,
    %minor y tick num=1,
    ytick align=outside,
    %yminorgrids=true,
]
    \addplot [black,very thick,domain=0:70]  {rnd+x*0.3};
%
    \node at (axis cs: 56,12) {\Large{\myfont $0.4$\,MPa}};
%
    \draw [semithick] (30,8.9) -- (45,8.9);
%
    \draw [semithick] (45,8.9) -- (45,13.8);
%
\end{axis}
\end{tikzpicture}
}

\end{figure}


\end{document}

enter image description here

0

I solved it using \foreground and \background sets and just inserted a white rectangle to cover the line of the main graph. Here is the complete code and the outcome.

\documentclass[a4paper]{article}

\usepackage{amsmath}
\usepackage{pgfplots}
\usepackage{pgfplotstable}%fitting functions
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\pgfkeys{/pgf/number format/.cd,1000 sep={}}


\newcommand{\myfont}{\fontfamily{cmss}\fontseries{ul}\selectfont}% used with mathpazo


\pgfplotsset{compat=1.15} \pgfplotsset{label style={font=\Large},
            tick label style={font=\Large}}

\pgfplotsset{error bars/.cd,
    x dir=both, x explicit,
    y dir=both, y explicit,
    }
\pgfplotsset{/pgfplots/error bars/error bar style={semithick,black}}
%\pgfplotsset{/pgfplots/label shift={0pt}}


\begin{document}


\begin{figure}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\centering
\begin{tikzpicture}
\begin{pgfonlayer}{background}
\begin{axis}[
    name=plot1,
    enable tick line clipping=false, % makes all ticks same width
    axis line style=semithick,
    width=9cm,
    height=7cm,
    legend style={at={(0.95,0.35)},draw=none},
    legend cell align={right},
    x tick style={black,semithick},
    x label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    xlabel={{$\lambda^2-\dfrac{1}{\lambda}$} [{\myfont -}]},
    xmin=0,xmax=70,
    xtick={0,10,...,70},
    xtick pos=bottom,
    xtick align=outside,
    y tick style={black,semithick},
    y label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    ylabel={{$\sigma_t$} [{\myfont MPa}]},
    ymin=0,ymax=30,
    ytick={0,10,...,30},
    ytick pos=left,
    ytick align=outside,
]
    \addplot [black,thick] file {tensilePPmain.txt};

    \node at (axis cs: 55,11.5) {\large{\myfont $0.4$\,MPa}};

    \draw [semithick] (30,8.9) -- (45,8.9);

    \draw [semithick] (45,8.9) -- (45,13.8);

\end{axis}
\end{pgfonlayer}
%\end{tikzpicture}



%\begin{tikzpicture}
\begin{pgfonlayer}{foreground}
\begin{axis}[
    at={(100,310)},
    enable tick line clipping=false, % makes all ticks same width
    axis line style=semithick,
    width=5cm,
    height=4cm,
    axis background style={fill=white},
    legend style={at={(0.95,0.35)},draw=none},
    legend cell align={right},
    x tick style={black,semithick},
    x label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    xlabel={{$\epsilon_n$} [{\myfont -}]},
    xmin=0,xmax=2,
    xtick={0,1,...,2},
    xtick pos=bottom,
    xtick align=outside,
    y tick style={black,semithick},
    y label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    ylabel={{$\sigma_n$} [{\myfont MPa}]},
    ymin=0,ymax=2,
    ytick={0,1,...,2},
    ytick pos=left,
    ytick align=outside,
    %at=(plot1.axis cs:0,0),    
]
    \addplot [black,thick] file {tensilePPinset.txt};

    \addplot[mark=x,mark options={scale=2,thick}] coordinates {(0.52,0.74)};

    \node at (axis cs: 1,1.5) {\large{\myfont $\sigma_{y0}=0.8$\,MPa}};

    \draw [semithick,dashed] (0,0.74) -- (0.52,0.74);

    \draw [semithick,dashed] (0.53,0) -- (0.53,0.74);

\end{axis}
\end{pgfonlayer}

\draw [white,ultra thin,fill=white] (0.2,5.3) rectangle (5.5,5.5);

\end{tikzpicture}
\end{figure}


\end{document}

enter image description here

It does solve the problem, however I wouldn't mind to see more clever solutions.

You must log in to answer this question.

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