将插图放入 pgfplots 的主图中

将插图放入 pgfplots 的主图中

我正在尝试使用 pgfplots 在主图中插入插图。以下是我目前得到的结果。在此处输入图片描述

我希望结果如下图所示。最简单的方法是什么?在此处输入图片描述

以下是我迄今为止编写的代码

    \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}

答案1

您完全走在正确的轨道上。您(只是)需要将两个axis环境放在同一个tikzpicture环境中。

(请注意,我大大简化了您的代码,因此更加明显地表明需要做什么才能实现您的目标。)

% 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}

该图显示了上述代码的结果

答案2

将两个axis环境放在同一个tikzpicture环境中。但是,您无法plot1.axis cs:..像尝试的那样使用它,而是plot1将其变成具有特定锚点的节点。因此,例如,您可以使用at=(plot1.north west)它将第二个轴的锚点放置在第一个轴的左上角。

但是,在第一个轴上定义一个辅助坐标可能会更方便,例如\coordinate (otheraxispos) at (20,30);,然后anchor=north west,at=(otheraxispos)在第二个轴的选项中使用。

因为我没有您的数据文件,所以我用了一些随机数代替。我还减小了字体大小和插入轴的大小。

要在 inset 后面填充白色,您可以\fill[white] (otheraxispos) rectangle +(4.5cm,-3.5cm);在第二个环境之前添加,并为第二个axis设置。这不是特别优雅,但至少可以工作。这里的和对应于为 inset 定义的宽度和高度。anchor=outer north westaxis4.5cm3.5cmaxis

(如手册第 4.10.1 节所述常见的扩展选项这些尺寸只是近似值,因此您可能需要/必须稍微更改它们。

在此处输入图片描述

\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

这里我使用了 中的\stackinset宏来stackengine完成。我还通过 将插图缩小到 70% \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}

在此处输入图片描述

答案4

我使用 \foreground 和 \background 集解决了这个问题,只需插入一个白色矩形来覆盖主图的线。以下是完整的代码和结果。

\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}

在此处输入图片描述

它确实解决了这个问题,但我并不介意看到更巧妙的解决方案。

相关内容